I2C does not work. OS Error

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
holgerkraehe
Posts: 3
Joined: Mon Jan 30, 2017 9:04 pm

I2C does not work. OS Error

Post by holgerkraehe » Mon Jan 30, 2017 9:09 pm

Hello

I tried the following:

[code]
MicroPython v1.8.7-7-gb5a1a20a3 on 2017-01-09; ESP module with ESP8266
Type "help()" for more information.
>>> from machine import Pin, I2C
>>> gpio_scl=Pin(12)
>>> gpio_sda=Pin(14)
>>> i2c = I2C(scl=gpio_scl, sda=gpio_sda, freq=100000)
>>> addr = 0x03
>>> ch0 = '\x00'
>>> i2c.writeto(addr,ch0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 19] ENODEV
>>>
[/code]


Could someone help my to figure out, what the problem is?
Thanks

User avatar
kfricke
Posts: 342
Joined: Mon May 05, 2014 9:13 am
Location: Germany

Re: I2C does not work. OS Error

Post by kfricke » Tue Jan 31, 2017 12:22 am

What does I2C.scan() show?

holgerkraehe
Posts: 3
Joined: Mon Jan 30, 2017 9:04 pm

Re: I2C does not work. OS Error

Post by holgerkraehe » Tue Jan 31, 2017 5:44 am

[quote="kfricke"]What does I2C.scan() show?[/quote]

Hi

Thanks for your answer.
I have my own hardware at the I2C Bus.
Therefore i2c.scan() will probably not show up something.

My hardware is a microcontroller (STM8) which is configured as I2C slave (hardware i2c not software).
It should only receive data from the ESP. Sure, it will also answer with NACK and ACK.

But what does this error mean: OSError: [Errno 19] ENODEV ?

Thanks

User avatar
deshipu
Posts: 1385
Joined: Thu May 28, 2015 5:54 pm

Re: I2C does not work. OS Error

Post by deshipu » Tue Jan 31, 2017 8:43 am

That error means that the ESP8266 has sent a start condition followed by the address of the device, but didn't receive an ACK after that.

The reasons for that may be many, but the most common one is that the i2c device is connected wrong -- for example the wires are swapped, there are no pullup resistors or the resistors are too weak, etc.

It's also possible, that the device in question simply didn't answer because it has a different address than the one you are trying (i2c.scan() should help diagnose that), has a bug in its code, expects a slower clock frequency (you can set that with the frequency parameter) or clock stretching timeout (you can set that with the timeout parameter), etc.

Unless you give us more details and check the things we asked you to check, we can just be guessing...

tech1337
Posts: 2
Joined: Mon Feb 13, 2017 12:58 am

Re: I2C does not work. OS Error

Post by tech1337 » Mon Feb 13, 2017 1:02 am

I was also baffled by this trivial error at first until I realised it is a simple factor of too much knowledge!

The I2C.scan() function returns the addresses not in Hex, but in good ol' decimal! I was getting the result [30] from the scan and trying to access the device on 0x30, turns out that it is at the conversion to hex of 30 decimal, 0x1E!

Try that the next time you get stuck with I2C addresses! So simple, but basically ruined my day until I realized!

User avatar
pythoncoder
Posts: 4935
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: I2C does not work. OS Error

Post by pythoncoder » Mon Feb 13, 2017 9:16 am

That is how the Python language works. scan() returns a list of integers. Integers are displayed in decimal by default: if you want to convert them to a hex string, you need to issue hex(my_integer).
Peter Hinch

sara
Posts: 8
Joined: Thu Oct 03, 2019 12:26 pm

Re: I2C does not work. OS Error

Post by sara » Thu Oct 24, 2019 12:29 pm

I had a similar problem (also with I2C interface) and came here trying to figure out what Error 19 could be...
Thanks to you I realized I was not using the pull up resistors and now it's all fine. I am used to use "plug and play" modules that come with pull-up resistors, and now that I am building my own circuit I just forgot it...
Thanks

happyandrew
Posts: 4
Joined: Wed Feb 17, 2021 4:11 pm

Re: I2C does not work. OS Error

Post by happyandrew » Wed Feb 17, 2021 4:27 pm

I am having the same problem, I have a pi piclo running microPython, with this simple code.

import machine
import utime

sda=machine.Pin(4)
scl=machine.Pin(5)
i2c=machine.I2C(0, sda=sda, scl=scl, freq=40000)

print(i2c.scan()) # this displays [39]
print("I2C Hex address "+ hex(i2c.scan()[0}).upper()) # this displays I2C address 0x27
i2c.reafrom(0x27,4)
print (i2c) #this displays I2C(0, freq399361, scl=5, sda=4

i2c writeto(0x27, '\x7C')
i2c.writeto(0x27, '\x2D')

i2c writeto(x,27, "Hello")

I get no errors, but nothing happens to the LCD. If I put a longer string "Hello world" then the display just goes blank. Exactly the same thing happens when I put in 39 in place of the 0x27. I have now spent 3 days and bought 3 new i2c LCD's from amazon and going nuts!

User avatar
dhylands
Posts: 3593
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: I2C does not work. OS Error

Post by dhylands » Wed Feb 17, 2021 5:42 pm

Which I2C LCD do you have?

happyandrew
Posts: 4
Joined: Wed Feb 17, 2021 4:11 pm

Re: I2C does not work. OS Error

Post by happyandrew » Wed Feb 17, 2021 6:16 pm

Gosh! I got two 1602 serial HD44780 and Geeek 2004 20X4. That mean anything ?

Post Reply