Ceci est une ancienne révision du document !
Welcome back! Every once and a while, I’ll be doing a review of a MicroController of some sort that I have tested. I don’t get any free devices from anyone, so if I can afford one, I’m pretty sure that you can too. This month, we have a lot to cover, so let’s get started right away.
Cytron Maker Pi Pico A day or so after the release of the RPi Pico, I found out about this product. Cytron (https://www.cytron.io/c-development-tools/c-arm-development-tool/p-maker-pi-pico) is a really innovative company based in Malaysia. Unfortunately, when I found out about it, it was already sold out even though it was still in beta. I waited, and the next batch came in, but was sold out again. Finally, I was able to get one of the third batch of devices and, believe me, it was worth the wait! They were sold out again within a few hours.
The device includes a development board with a Pico already soldered onto it. Each pin is brought out on the board as a male header pin, and every GPIO pin has an LED. There are 6 Grove connectors, 3 buttons, a buzzer, an SD Card slot, a dedicated port for an ESP-01 WiFi card, a NeoPixel RGB LED, and more. All of that for just under $10 USD, and has a 1-year warranty! Shipping (to the U.S. at least) is reasonable and fairly fast. Please be aware, the ESP-01, MicroSD card, and jumpers, are not included with the board. Once I got it and plugged it in, I was surprised that a demo program was already flashed to the Pico. At first glance, the demo highlights the GPIO LEDs by walking through each LED quickly turning them on then off while the buzzer plays a short snippet from the iconic Mario theme song. Being curious, and fairly impressed by the fact that the demo was already there, I tried pressing each of the three buttons. Sure enough there was more demo to be seen. Button 1 toggles all of the GPIO LEDs. Button 2 runs through all of the NeoPixel colors, and Button 3 plays the Mario theme while slowly melding the NeoPixel color shift.
I have to say that one of the things that I wanted the board for, was to learn more about the NeoPixel RGB LED. I always thought that they were pretty cool, but not cool enough to spend the money to get one of the various types. I really couldn’t come up with a compelling project to justify the purchase. When the demo got to the parts that controlled the NeoPixel, it was so bright that I couldn’t look directly at it. I have created a demo for using the NeoPixel that allows the NeoPixel brightness to be controlled. We’ll take a look at it next month. The good people at Cytron have set up a github repository with example code for both MicroPython and CircuitPython. You can find it at https://github.com/CytronTechnologies/MAKER-PI-PICO. While there aren’t examples for everything that the board can do, for the most part you will find plenty of things to keep you busy for a while. The one frustrating thing is that while the basic capabilities of the ESP-01 will connect to the local network wirelessly, going further than that doesn’t seem to work. I will keep trying to come with some working code for you in the next month or so.
Over all, this is a great way to get into using the RPi Pico – with lots of exploration possibilities for a very low cost. It’s especially good for a younger budding scientist to learn since there is no soldering involved. Project of the month Last month, we went over a couple of simple “get started” projects that involved the Pico and making the onboard LED do things. Moving from the onboard LED to an offboard is not much more involved, and there’s a lot of information out on the web to do this. I’ll let you research that for yourself. I can, however, point you to a good project direct from the Raspberry Pi Foundation. While designed for young people, it gives you a step-by-step introduction to the Pico and MicroPython Programming. You can find it at https://projects.raspberrypi.org/en/projects/getting-started-with-the-pico.
Now for our project. We’ll be dealing with a real world sensor on the Pico. To connect the sensor to the Pico, we will be using I2C. For an overview of I2C, you might want to set your wayback machine to Full Circle Magazine issue # 112 in August 2016. Basically, I2C is a specialized 2-wire serial communication protocol that allows multiple devices to exist on the same two-wire buss. Each I2C device has an address that we use to talk to that individual device. This month, we’ll work with the Pico as our Microcontroler, a BMP180 Temperature, Pressure and Altitude sensor, and an OLED display. Both the display and sensor communicate to the Pico via I2C, which makes the wiring very easy.
As you can see from the wiring diagram shown left, there are only 4 wires needed for each device and they all use the same connection points on the breadboard. To make it easy for you, I’ve created a simple table for the connections to the Raspberry Pi Pico. I’ve tried to make the wire colors consistent with “Normal” conventions. Red for positive voltage, black for ground, white for data, and yellow for clock signals. I suggest that when you make your connections, you do it with the Pico unplugged from your computer. Once you plug it in, fire up Thonny. Whenever I work on a project that uses I2C devices, the first thing I do is to make sure that the Microcontroller that I’m using sees the sensors and/or displays that I plan to use. Sometimes, the jumper wires don’t quite make good contact in the breadboard. This isn’t all that unusual. To do this, I have created a small program (top right) that I keep on the Pico.
The first thing is to import the machine library from MicroPython. In this program, I assign the I2C pins right away. Since I’m going to be using the I2C buss 0, I’m using GP8 pin (physical pin 11) for the data line (SDA) and the GP9 pin (physical pin 12) for the clock line (SCL). Next, I define the actual buss and create the i2c object. Then I use the i2c.scan method to get a list of the addresses that exist on the buss, and then print that list. Notice that I am using the hex value of the device. When I run it, I get back the following information… MicroPython v1.14 on 2021-03-24; Raspberry Pi Pico with RP2040 Type “help()” for more information. »> %Run -c $EDITOR_CONTENT 0x39 0x3c 0x77 »>
So what do the returned numbers mean? Each of the numbers represent the I2C address for each device on the buss that was found. For this run, the addresses represent the following devices (see image below) Right now, you probably are wondering why I have a TSL2561 luminosity sensor on the buss as well. That’s because the actual breakout board that I have is one that is a number of years old and is a 4-in-1 device that includes the BMP180 and the TSL2561 all together. A number of people whom I’ve talked to have a BMP180 and a BMP280 both. And both are very similar in appearance. This leads to a huge amount of confusion, since the driver library for the BMP180 won’t work with the BMP280, and vice versa. In order to combat this confusion, you can verify which device you are using by doing a simple REPL check by reading memory location 0xD0 on the device. If you get back a 0x55, you are using a BMP180. If, on the other hand, you get back an 0x58, you are using a BMP280. This information comes from the datasheet for the devices. »> from machine import Pin, I2C »> i2c=I2C(0) »> i2c.readfrom_mem(0x77, 0xD0, 2)[0] 85 »> print(hex(85)) 0x55 »>
If you are paying close attention to the code that I’ve presented so far, you will notice a difference in the way that I’m creating the i2c object. The method that I presented in the i2cscan program is the “official” way of doing it. The one that I present in the memory-read way is a somewhat newer way and is much easier. I have a tendency to use the second way, since it uses all of the defaults without having to explicitly define each parameter. I presented the first, so you know the “proper” way, and if you need to change the pin assignments or modify the frequency of the buss up or down, you already have the information. Now that we have verified the existence of the sensor and display, and have everything wired up, let’s get into the code (top right). Download the three files (bmp180.py, BMP180-OLED.py and ssd1306.py) from my github repository (see below) to your local computer. Then you will need to copy the file “bmp180.py” and “ssd1306.py” onto your Pico. You can use the File|Save Copy function in Thonny. The driver libraries have to be on the Pico. While you can run the actual program from your local computer, I suggest that you copy it to the Pico as well, just to keep everything together.
As with any of our Python files, you need to start with the imports that are needed for the program (bottom right). Next, we will set the width and height of our OLED display device. The next three lines deal with configuring the BMP180 sensor. The line that is commented out is the “default” baseline for the barometric sensor. I modified it for where I live in Texas. Your “mileage” will vary. You can get more information on this from https://www.circuitbasics.com/set-bmp180-barometric-pressure-sensor-arduino/ bmp180.oversample_sett = 2 # bmp180.baseline = 101325 bmp180.baseline = 102032 # Modified for my location in Texas
At this point, we’ll clear the OLED display. The .fill(0) command sets all the pixels to 0, which is off or black. Then we’ll set some text starting at column 5, row 5 of the display. We must use the .show() method to actually display the changes. This line will stay the same throughout the run of the program. # Clear the OLED Display oled.fill(0) # Send the header to the OLED oled.text(“BMP180 Demo”,5,5) oled.show() We will now create a “forever” loop (next page, top right) that gets the temperature, pressure and altitude values from the BMP180 and display the temperature on the OLED display.
Since I don’t do metric very well without having to do a lot of thinking, I’ve added a few lines to deal with the conversions for me. The tempf is the Fahrenheit value from the Celsius value. The variable p is the base pressure reported by the BMP180, which I convert to inches of mercury from the default hectoPascals (hPa). If you want a different output unit of measure, you can again refer to the above mentioned web site. Finally (bottom right), I convert the altitude from meters to feet. Notice that this value seems to change quite a bit, but for our purposes it is close enough. The last few lines of the program provide the output. We print to the REPL terminal the four values Temperature Celsius, Temperature Fahrenheit, Barometric Pressure, and Altitude. It would be much easier if MicroPython supported the Python f-strings, but we live with what we can get. You will notice that there are two lines that output to the OLED display that are very similar. The first prints the temperature at column 5, row 23 and the second prints the same thing in black (with the last parameter as 0), which erases the text after the sleep interval of 2 seconds. With a bit of experimentation, you could modify the column value to only erase the actual temperature value.
Your output in Thonny should look something like this… Temp: 23.78C TempF: 74.80 Pressure: 29.78 Altitude 929.1451 Temp: 23.78C TempF: 74.80 Pressure: 29.78 Altitude 931.2545 Temp: 23.79C TempF: 74.82 Pressure: 29.78 Altitude 930.3514 Temp: 23.79C TempF: 74.82 Pressure: 29.78 Altitude 928.817 Temp: 23.79C TempF: 74.82 Pressure: 29.78 Altitude 928.817 Temp: 23.79C TempF: 74.82 Pressure: 29.78 Altitude 929.2014 Temp: 23.80C TempF: 74.84 Pressure: 29.78 Altitude 928.2951 Temp: 23.80C TempF: 74.84 Pressure: 29.78 Altitude 930.7858 Temp: 23.81C TempF: 74.85 Pressure: 29.78 Altitude 930.4326 … Known displays and sensors for the RPi Pico I’ve been really busy testing various sensors and displays that I have on the Pico. I created a list of the sensors that I tested and where I got the driver libraries, since I seem to get a number of questions from people just starting with the Pico. All of these are 3.3volt compliant, and safe to use directly with the RPi Pico. Here is a list of some of the ones that I have verified and a link to the working driver library, where possible, along with any notes that I made during testing…
LSM303DLHC - Acceelerometer + Magnetometer - https://github.com/kamikaze/pyboard-examples/tree/master/imu (Works, but accuracy not verified) Si7021 - Temp/Humidity Sensor - https://github.com/robert-hh/SI7021 Requires small modification - https://github.com/gregwa1953/SI7021-MicroPython-RPi-Pico BMP180 - Temp/Pressure/Altitude Sensor https://github.com/micropython-IMU/micropython-bmp180 (You have to comment out line 47 of the driver self._bmp_i2c.start() to make it work on the Pico). Or you can can find already modified at https://github.com/gregwa1953/FCM168_MicroThisMicroThat DHT22 - Temp/Humidity https://github.com/danjperron/PicoDHT22 TSL2561 - Lux Sensor Works but unsure of values
IR-08H - Works, but not very sensitive. May need to be calibrated. http://irsensor.wizecode.com/ for information My Pinout - Enable, VCC Out, Gnd OLED1306 - OLED Display 128×32 and 128×64 modes https://github.com/gregwa1953/FCM168_MicroThisMicroThat LCM1602 - 16×2 LCD I2C https://github.com/Bhavithiran97/LCM1602-14_LCD_Library ESP-01 - WiFi - Connects to local network router, but can't get out past there. LCD 16×2 3 volt display https://github.com/Bhavithiran97/LCM1602-14_LCD_Library I will try to set up pages on my github repository for each of these (one has already been done) as time allows.
Interesting Website information on Microcontrollers (Pico Based) I wanted to provide you with some links to interesting websites that have news and projects about the RPi Pico. https://www.raspberrypi.org/documentation/rp2040/getting-started/ You can also check out Tom’s Hardware website, which has multiple postings about the Pico, as well as the Raspberry Pi and other Microcontrollers - https://www.tomshardware.com/
Looking to the future In the coming months, I’ll be talking about CircuitPython on the RPi Pico, the Sparkfun ESP32 Thing Plus, the NodeMCU ESP8266, various controllers and displays, connecting the RPi Pico to a MQTT broker either on your network or on the Internet, and much more. My goal here is to provide information about various MicroControllers, sensors and displays that are inexpensive and easy to connect to the various boards. You can find all the code and images from this article at my github repository: https://github.com/gregwa1953/FCM168_MicroThisMicroThat Until next time, as always; stay safe, healthy, positive and creative!