These itty-bitty displays are wonderful. So easy to connect the 4 leads for I2C. See the IIC Address Select on the back? The factory soldered a little short to give the board one address or another.
Are we supposed to look at all of them? People can add or remove these jumpers and change the address. Let's make an I2C scanner to test for sure. Also check for broken components.
Like an Arduino Uno but faster, bigger program space. This is an ARM Cortex-M processor. More like a Xiao or Pico. This project should work on any version of the board. Identify your hardware by labels and component layout.
Built in buzzer, user buttons, LED module. This is an electronics laboratory ready to work out of the box.
I2C Default Pins 19 and 20The name comes from integrated circuit to integrated circuit connections using a two wire connection: IC to IC = I2C or IIC. Sometimes you see I2S for sound. Arduino boards have this, too. One SDA lead is for data, one SCL for clock.
You can talk to up to 127 addresses on these two pins. Fast enough to run the OLED screens with 128x64 pixel resolution. Look for sensor kits and projects, many components are I2C.
Device ManagerConnect your micro:bit to your PC. To work properly the PC will install drivers so that it appears as a comms port and as a storage device. COM19 on my computer means we can talk to our little board on a hyperterminal serial connection through USB.
The Micro:Bit also shows up on your computer as a USB drive where you can just drop your hex file directly onto the board. CMSIS-DAP is a debugging interface that lets you see inside the processor.
Python!Go to webpage https://python.microbit.org/v/3. Use this code:
# scan i2c bus for responding devices
from microbit import *
start = 0x00
end = 0x7F
while True:
print("Scanning I2C bus...now..")
for i in range(start, end + 1):
try:
i2c.read(i, 1)
except OSError:
pass
else:
print("Found: [%s]" % hex(i))
display.scroll(hex(i))
print("Scanning done")
sleep(1)
Click the play button and watch the display. Our script is interpreted by the simulated board. It thinks every address is populated. Our script is good.
Notice the %s format instruction in the print command. Find the Show serial feature on the webpage. We are telling our python operating system to use string formatting for our hexadecimal values.
DownloadDownload to your Micro:Bit board with Send to micro:bit or Save to download folder and drag-and-drop. Watch the led on the back of the board and your PC screen for progress messages. Check your downloads folder and delete old hex files.
HyperterminalWindows used to come with a program called hyperterminal for dial-up modems and text user interfaces. Tera Term is a program you can install in your computer. Linux has Putty. Port number shown in device manager, default speed is 115200 baud. Linux $ ls /dev/tty* to see port.
Addresses vary with the hardware release. This board has an accelerometer at address 0x1D and a magnetometer(compass) at address 0xe. This identifies my board as a Micro:Bit version 1.3. Newer boards have different addresses.
Careful to match the four leads VCC-GND-SDA-SCL. 3.3volts can still damage components. Data SDA and clock SCL are normally pins 19 and 20 on our Micro:Bit board card edge. The microbits site uses this as a standard connection for I2C devices such as our OLED display.
We won't be sending any picture to this module but we want to scan its factory set I2C address. We can do this project with any 7bit I2C sensor module.
This time we see an added, third device. It is our little display. We can talk to it using address 0x3C hexadecimal. You will often see this address pre-selected in Arduino sample code.
Change computer and you may see a different comms port address. Check in your device manager to see that the PC is talking to the board.
Blocks EditorWe have everything connected up. Let's see how to write the same code in Blocks. Open the online Micro:Bit blocks editor. Make this forever loop.
This code writes I2C messages out the default pins, forever. It sends to address zero, then one all the way up to address 127 to start again. You don't need any extensions for this program.
If ConditionThis pretty block is the real function for our scanner program. If an address replies back with any answer then we perform two actions: show the number on the little display screen and serial write value to print out the serial monitor port.
Open hyperterminal or equivalent for the same port, same speed settings. I see a problem. We are printing decimal numbers for the two devices inside of our micro:bit board.
If we edit this screen to use the hexadecimal notation 0x7F for the address range it keeps defaulting back to decimal.
We see decimal numbers when we need to see hex to be more useful. Other programmers say to add an extra routine to generate hexadecimal numbers and print them as text strings.
The web interface used by blocks defaults to decimal digits to protect the programmer from mistakes. This is a user-friendly convenience that gets in our way when we really want to use hexadecimal.
Comments