Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i2cdetect in the read direction hangs/crashes after finding a sensor #622

Open
sp4ghet opened this issue Jan 1, 2025 · 2 comments
Open

Comments

@sp4ghet
Copy link

sp4ghet commented Jan 1, 2025

I'm using an Arduino Mega 2560 R3 with a MH-Z16 CO2 sensor in I2C mode

I basically just took the sample for I2Cdetect verbatim (just swapped the order of Read/Write)

#![no_std]
#![no_main]

use arduino_hal::{prelude::*, I2c};
use panic_halt as _;

const NDIR_ADDR: u8 = 0x4D;

#[arduino_hal::entry]
fn main() -> ! {
    let dp = arduino_hal::Peripherals::take().unwrap();
    let pins = arduino_hal::pins!(dp);

    let mut serial = arduino_hal::default_serial!(dp, pins, 57600);

    let mut led = pins.d13.into_output();
    let mut i2c = arduino_hal::I2c::new(
        dp.TWI,
        pins.d20.into_pull_up_input(),
        pins.d21.into_pull_up_input(),
        100000,
    );

    ufmt::uwriteln!(&mut serial, "Read direction test:\r").unwrap_infallible();
    i2c.i2cdetect(&mut serial, arduino_hal::i2c::Direction::Read)
        .unwrap_infallible();

    ufmt::uwriteln!(&mut serial, "Write direction test:\r").unwrap_infallible();
    i2c.i2cdetect(&mut serial, arduino_hal::i2c::Direction::Write)
        .unwrap_infallible();

    loop{};
}
/// serial output
Read direction test:
-    0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:       -- -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- 4d

It seems to run until it finds the sensor, and then it freezes.
the Write direction seems to run fine, but the read direction hangs.
I've tried adding some code below the i2cdetect but it doesn't appear as if any of it actually runs.

I've tried using the normal Arduino/C++ code provided by the manufacturer and it worked (connected and sent measurements over serial) so I don't think I've setup the hardware incorrectly, but please let me know if I fumbled something obvious.

I also tried just calling ping and it seemed to behave the same way (unsurprisingly..) where the ping goes through, but all following calls to i2c hang/panic(?)

    let pinged = i2c
         .ping_device(NDIR_ADDR, arduino_hal::i2c::Direction::Read)
         .unwrap();
    ufmt::uwriteln!(&mut serial, "Pinged: {}\r", if pinged { 1 } else { 0 }).unwrap();

    ufmt::uwriteln!(&mut serial, "Read direction test:\r").unwrap_infallible();
// this code doesn't run (except the first row of labels)
    i2c.i2cdetect(&mut serial, arduino_hal::i2c::Direction::Read)
        .unwrap_infallible();
@sp4ghet
Copy link
Author

sp4ghet commented Jan 1, 2025

@stappersg
Copy link
Contributor

My advice to sp4ghet:

  • Close this issue
  • cargo generate --git https://github.com/Rahix/avr-hal-template.git --name ndir_mvp
  • Keep the mvp a minimum viable product, meaning: Stay away from swapping the order of Read/Write and other let me change this now, I will need it later away
  • Check, double check and triple check the hardware setup. That implies having a schematic of the electronics. And above all: Understand all the hardware involved
  • Document your prototype further. Do it for your future self and do it for your current self that is seeking help from others.
  • Enjoy the challenge of getting to work what you envision
  • Persist
  • Smile humbly upon reaching your goal
  • Maybe publish the achievement (It is 2025, there is something called Internet, every one is a publisher.)

My advice to everyone seeking help with their project:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants