Skip to content

Blank I2C output ESP32 simple program #910

@drbh

Description

@drbh
Chip type:         esp32 (revision v1.0)
Crystal frequency: 40MHz
Flash size:        4MB
Peripheral:        MLX90614
VCC:               3V3
GND:               GND
SDA:               GPIO 21
SCL:               GPIO 22

Thank you for the awesome library. I am having issues connecting a MLX90614 (on a breakout board with 4.7K pull-up resistors on sda and scl) to my ESP32.

I've been able to validate that the device works via the Ardunio IDE using this library and this program.

#include <Adafruit_MLX90614.h>

Adafruit_MLX90614 mlx = Adafruit_MLX90614();

void setup() {
  Serial.begin(115200);
  while (!Serial);

  if (!mlx.begin()) {
    Serial.println("Error connecting to MLX sensor. Check wiring.");
    while (1);
  };
}

void loop() {
  Serial.print("Ambient temperature = "); 
  Serial.print(mlx.readAmbientTempC());
  Serial.print("°C");      
  Serial.print("   ");
  Serial.print("Object temperature = "); 
  Serial.print(mlx.readObjectTempC()); 
  Serial.println("°C");

  Serial.println("-----------------------------------------------------------------");
  delay(500);
}

However I cannot seem to get a useful response when trying to communicate with the I2C bus via esp-hal

I've tried to manually write_read at the address 0x5A and always get a response of [ff, ff, ff].

Additionally I've attempted to scan for all I2C devices and send all 1 byte messages and always get a response of [00, 00, 00] or [ff, ff, ff]. Please see the example code below.

I am an embedded newbie so its very likely I'm doing something (or everything) completely wrong. Is there a better/correct way to read data from an I2C bus?

#![no_std]
#![no_main]

use esp32_hal::{
    clock::{ClockControl, Clocks},
    entry,
    gpio::{Gpio6, GpioPin, Output, PushPull, Unknown, IO},
    i2c::I2C,
    interrupt,
    peripherals::{Peripherals, I2C0, I2C1, RTC_I2C},
    prelude::*,
    Delay,
};
use esp_backtrace as _;
use esp_println::println;

#[entry]
fn main() -> ! {
    let peripherals = Peripherals::take();
    let system = peripherals.SYSTEM.split();
    let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
    let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);

    let sda = io.pins.gpio21;
    let scl = io.pins.gpio22;

    let mut i2c = I2C::new(peripherals.I2C0, sda, scl, 100u32.kHz(), &clocks);

    for addr in 0..=127 {
        if let Ok(_) = i2c.write(addr, &[0x07]) {
            println!("Found device at address: 0x{:02X}", addr);
            for msg in 0..=255 {
                let to_send: [u8; 1] = [msg];
                let mut data = [0u8; 3];
                i2c.write_read(addr, &to_send, &mut data).ok();
                println!("Sent: 0x{:02X}, Received: {:02x?}", msg, data);
            }
        }
    }

    loop {}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingperipheral:i2cI2C peripheral

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions