i2c woe on pyboard d
Posted: Fri May 17, 2019 3:44 pm
Been pulling my hair out trying to get I2C working on the new board. And I mean this in terms of following examples and documentation. In a nut shell - can someone point me to a clearly written piece of documentation that 'just works' in doing something like:
So what I would love is the document to specify how to construct the i2c object! . And breaking this down to individual points this is what I don't get:
1) Should i use I2C from the pyb library or machine library? Which is correct and best way?
Python rule 13 -There should be one-- and preferably only one --obvious way to do it.
2) From the machine library here (http://docs.micropython.org/en/latest/l ... e.I2C.html) it says that the class is constructed like this: class machine.I2C(id=-1, *, scl, sda, freq=400000)
Does this mean that we have 2 key words that are defaulted if you don't provide them which are id and freq? So you can construct the object by just providing the scl and sda arguments right? And those arguments have to be pin objects specifying the pin to use? So why does this work:
nothing worse than doing something that works but you have no idea why it works..... I just supplied my class with a single parameter and it was a string not even a pin object....
3) If I try and create an I2C object and provide it with the correct values it does not seem to work for me, maybe someone can spot my school boy error!!!! ....
So i need to pass in a string to the Pin constructors. Although it looks like the doc's are passing in integer values?
So from here: ( http://docs.micropython.org/en/latest/l ... e.Pin.html)
It says:
That confused me, but I changed it to provide string values for I2C - which is shown below:
This stumped me as I've provided id, scl, sda and freq. Why does it thing I've provided an extra keyword arg?
4) I'm guessing this, but for the pyboard 'D' it's already setup to use pin X9 and X10 is this correct?
OK - I'm off for a coffee and some chicken... Please someone cheer me up and answer this post!
Kind regards, Nicholas.
Code: Select all
>>> i2c.scan()
[60]
1) Should i use I2C from the pyb library or machine library? Which is correct and best way?
Python rule 13 -There should be one-- and preferably only one --obvious way to do it.
2) From the machine library here (http://docs.micropython.org/en/latest/l ... e.I2C.html) it says that the class is constructed like this: class machine.I2C(id=-1, *, scl, sda, freq=400000)
Does this mean that we have 2 key words that are defaulted if you don't provide them which are id and freq? So you can construct the object by just providing the scl and sda arguments right? And those arguments have to be pin objects specifying the pin to use? So why does this work:
Code: Select all
>>> import machine
>>> i2c = machine.I2C('X')
>>> i2c.scan()
[60]
3) If I try and create an I2C object and provide it with the correct values it does not seem to work for me, maybe someone can spot my school boy error!!!! ....
Code: Select all
>>> i2c = I2C(id=-1, scl=machine.Pin(9), sda=machine.Pin(10), freq=400000)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't convert 'int' object to str implicitly
So from here: ( http://docs.micropython.org/en/latest/l ... e.Pin.html)
It says:
Code: Select all
from machine import Pin
# create an output pin on pin #0
p0 = Pin(0, Pin.OUT)
Code: Select all
>>> i2c = I2C(id=-1, scl=machine.Pin('X9'), sda=machine.Pin('X10'), freq=400000)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: extra keyword arguments given
4) I'm guessing this, but for the pyboard 'D' it's already setup to use pin X9 and X10 is this correct?
OK - I'm off for a coffee and some chicken... Please someone cheer me up and answer this post!
Kind regards, Nicholas.