-1

i was following the Getting Started with MicroPython on Raspberry Pi Pico book and i am stuck at page 120, the example code did not work for some reason.

I am coding in VS Code, but just in case i tried running it in Thonny, still without any luck. I got the error(s):

  • Argument of type "Literal['|']" cannot be assigned to parameter "buf" of type "bytes" in function "writeto"   "Literal['|']" is incompatible with "bytes"
  • Argument of type "Literal['hello world']" cannot be assigned to parameter "buf" of type "bytes" in function "writeto"   "Literal['hello world']" is incompatible with "bytes"
  • Argument of type "Literal['-']" cannot be assigned to parameter "buf" of type "bytes" in function "writeto"   "Literal['-']" is incompatible with "bytes"

Then the lcd's backlight gets shut off and the top row became all black boxes.

The LCD and the i2c adapter works with C code but just in case i added the 4k7 resistors to the buses. (under micropython it didn't work no matter if i add them or not)

import machine

sda = machine.Pin(0)
scl = machine.Pin(1)

i2c = machine.I2C(0, sda=sda, scl=scl, freq=400000)

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

Also there isn't any pre-written library for this? Why is C easier than micropython for some stuff? (I am using this exact same setup using C/C++ with the Arduino library, but every piece of hardware is the same /pico, i2c adapter, 1602 green lcd/)

bcseh202
  • 9
  • 5
  • String is not [bytes](https://docs.python.org/3/library/stdtypes.html#binary-sequence-types-bytes-bytearray-memoryview0. The book is for you to get started with the board, but it doesn't mean that you don't need to have a python book to learn the basic language. BTW, `machine` is already a pre-written library and with good documentation, so consult the [micropython documentation](https://docs.micropython.org/en/latest/library/machine.I2C.html) to understand how the library works and its APIs would benefit you for solving the problem. – hcheung Apr 17 '23 at 00:49

1 Answers1

0

It would be useful to check what version of MicroPython you’re running? Also, just to check what edition of the book are you reading (I see the same code on page 118 rather than 120). I know there was more than one edition of the book due to some corrections, and maybe some were missed.

If you look at the MicroPython documentation for the i2c.writeto method you’ll see that it takes a bytes value rather than a string, so the error you’re seeing is expected:


i2c.writeto(42, b'123')         # write 3 bytes to peripheral with 7-bit address 42
i2c.readfrom(42, 4)             # read 4 bytes from peripheral with 7-bit address 42

So, I’d guess that the leading b to indicate bytes, may have been dropped in the code printed in the book.

Andy Piper
  • 11,422
  • 2
  • 26
  • 49