BBC Micro:bit I2C Port Expander PCF8574

Questions and discussion about running MicroPython on a micro:bit board.
Target audience: MicroPython users with a micro:bit.
Post Reply
gowelding1
Posts: 2
Joined: Fri Sep 02, 2016 4:22 pm

BBC Micro:bit I2C Port Expander PCF8574

Post by gowelding1 » Sun Sep 04, 2016 6:12 pm

I have successfully connected this expanded to an Arduino and a Pi with no problem but the BBC proved to be very difficult as I could not find much information on programming the devices advanced features. I finally got it working but I am sure there must be an easier way of doing it as my knowledge of Python is limited. The first program reads the ports and the second program flashes an LED connected between port 0 and +V. Comments please.


from microbit import *

while True:
d=i2c.read(0x24, 1, repeat=True)
c=str(d)
tens=ord(c[4])
if tens >= 97:
tens=tens-87
else:
tens=tens-48

units=ord(c[5])
if units >= 97:
units=units-87
else:
units=units-48

val=tens*16+units

display.scroll(str(val),delay=400)

sleep(2000)

__________________________________________

from microbit import *

while True:
buf = bytearray(1)
buf[0]=0
i2c.write(0x24, buf, repeat=True)
sleep(1000)
buf[0]=1
i2c.write(0x24, buf, repeat=True)
sleep(1000)

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: BBC Micro:bit I2C Port Expander PCF8574

Post by SpotlightKid » Sun Sep 04, 2016 9:18 pm

Sorry, I got no help to offer on your actual question, but if you enclose the code in your post with [code]...[/code], it will display properly with indentation and will be much easier to read for someone inclined to look into it.

torwag
Posts: 220
Joined: Fri Dec 13, 2013 9:25 am

Re: BBC Micro:bit I2C Port Expander PCF8574

Post by torwag » Sun Sep 04, 2016 9:25 pm

New Users can't use tags since you could also use them to add images. Unfortunately, we received quite a few not very office friendly posts for approval. As soon as a new user reaches the limit which enables him/her to post without approval, tags are activated.
We are working on a solution to activate code tags only, but this is something not standard within the software we are using.

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: BBC Micro:bit I2C Port Expander PCF8574

Post by SpotlightKid » Sun Sep 04, 2016 9:35 pm

Ok, sorry, I didn't know that. Bit of a shame, really.

BBCode must die!

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

Re: BBC Micro:bit I2C Port Expander PCF8574

Post by pythoncoder » Mon Sep 05, 2016 9:24 am

@gowelding1 I haven't got your hardware so can't test this, but it looks like you're converting an ASCII hexadecimal text string to an integer. This can be done as follows:

Code: Select all

val = int(c[4:6], 16)
In your second example there isn't an obvious need to create the bytearray unless you're planning code which changes the contents dynamically. An alternative is to issue

Code: Select all

i2c.write(0x24, b'\0', repeat=True)
i2c.write(0x24, b'\1', repeat=True)
Here a bytes instance is created each time.
Peter Hinch
Index to my micropython libraries.

Post Reply