2

I am currently using an SRF-10 sensor connected to an esp32. It's being powered by 5V and I am using a level converter to bring down the voltage to 3.3V to be able to use it on the esp32. Both the SCL and SDA have a 1.8K pull up resistor as recommended on the datasheet. I have written the following script to try to get a read from the sensor. I am not entirely sure if it is correct but soon as it reaches line 16 I get an error saying [Errno 19] ENODEV. Eveything I could find suggests that the i2c connection isn't working properly but when I run i2c.scan() it returns the sensor address so I am guessing connections aren´t the problem. My script is as follows:

from machine import I2C, Pin
import time

byte = bytearray(4)
#Distance units
unit_in = 0x50
unit_cm = 0x51
unit_us = 0x52

i2c = I2C(scl=Pin(21), sda=Pin(22))
address = i2c.scan()[0]
print(address)

#Sensor range
range_mm = 11008 // 43 - 1
i2c.writeto_mem(range_mm, address, bytearray(2)) #line 16

#Begin reading
i2c.writeto_mem(unit_cm, address, bytearray(0))

#Reading after measurement
data = i2c.readfrom_mem(4, address, 0)
print(data)

This is the output:

112
Traceback (most recent call last):
  File "main.py", line 22, in <module>
OSError: [Errno 19] ENODEV
MicroPython v1.12 on 2019-12-20; ESP32 module with ESP32
Type "help()" for more information.

What could I be doing wrong?

1 Answers1

1

The proper use of i2c.writeto_mem requires the following order of arguments:

writeto_mem(addr, memaddr, buf, *, addrsize=8)

Therefore, try to use in line 16:

i2c.writeto_mem(address, range_mm, bytearray(2)) #line 16

Reference: class I2C

Jerzy D.
  • 6,707
  • 2
  • 16
  • 22
  • Thanks, I was able to make it work latter, I think I was looking at some old documentation. But thanks a lot, I should have probably said how I made it work but still I hope this can help someone else – Tomás Lopes May 27 '21 at 23:09