Week 9 : Output Devices
Summary
This week I tested adding an OLED screen to my board. To do this, I used I2C communication to send data to the screen. I tested first with a raspberry Pi Pico and then on my board, programming in python.
Assignments
Group Assignment
- Measure the power consumption of an output device.
Individual Assignments
- Add an output device to a microcontroller board you’ve designed, and program it to do something.
Measuring Power Consumption
This part was done in group and is accessible on the group page.
Adding Output Device to my Board
Contexte of the Week
This week I decided to continue with my final project. The first phase of development focuses on the development of a frequency-controllable high-voltage generator. Last week I developed a first version of the electrical board and ordered the various components.
This week I’ve decided to add an OLED screen to display various control information. This will be very useful for the final project.
To do this, I’ll need to use the I2C protocol to communicate with my display.
What’s I2C ?
I2C, which stands for Inter-Integrated Circuit and is said like “I-squared-C,” is basically a way for gadgets to talk to each other using just two wires, even if there are lots of them on the same bus. Think of it as a party line for electronics where:
- SDA (Serial Data Line): This is the line where all the gossip (data) gets passed around.
- SCL (Serial Clock Line): This is like the rhythm of the music at the party, keeping everyone in sync.
Why do people love I2C?
- Crowded Parties: You can have a bunch of devices chatting it up on the same two wires without missing a beat.
- Easy Setup: Only needing two wires keeps things simple and cheap, especially when adding more friends (devices) to the party.
- Flexible Guest List: You can invite or kick devices off the bus without ruining the vibe for everyone else.
In the DIY electronics world, I2C is everywhere because it’s super handy for making different parts, like sensors or displays, talk to your main controller without a wiring nightmare.
Adding OLED Display
Given that the manufacture of my electronic board has been delayed due to the delay in orders, I’ve decided to do the development on a raspberry pi pico. This will then be easily transferable to the Seeed RP2040.
The OLED screen I’m going to use needs to be powered by either 3V3. It must also have an I2C connection to my microcontroller , as explain before, for information transmission. These are the four pins I had planned for the previous week.
Programming OLED Display
I wanted to try out programming the Raspberry in python, so there are a few extra steps before I get into the coding part. This will enable you to write and run MicroPython scripts directly on your Pico from within VSCode.
Flashing MicroPython onto the Raspberry Pi Pico
First things first, I needed to get MicroPython onto my Pico. Here’s what I did:
-
Downloading MicroPython Firmware: I visited the official Raspberry Pi website and downloaded the latest MicroPython firmware
.uf2
file tailored for the Pico. -
Entering BOOTSEL Mode: With my Pico unplugged, I held down the BOOTSEL button and connected it to my computer via USB. This action put the Pico into BOOTSEL mode, ready to receive firmware updates.
-
Flashing the Firmware: I located the downloaded MicroPython firmware
.uf2
file and dragged it onto the Pico’s USB drive. The Pico rebooted automatically, indicating that the firmware had been successfully flashed.
Setting Up the Micro Pico Extension in Visual Studio Code
Now that MicroPython was running on my Pico, it was time to configure VSCode with the Micro Pico extension:
-
Installing Visual Studio Code: If I hadn’t already, I downloaded and installed Visual Studio Code from the official website.
-
Installing the Micro Pico Extension: I opened VSCode, navigated to the Extensions view, and searched for “Micro Pico” in the Extensions Marketplace. After installing it, I was ready to connect my Pico.
-
Connecting My Pico: I connected my Raspberry Pi Pico to my computer via USB.
-
Opening the Micro Pico Extension: I clicked on the Micro Pico icon in the sidebar or used the Command Palette to open it.
-
Selecting My Pico: If prompted, I selected my Pico from the list of available devices. The extension automatically detected my connected Pico.
With MicroPython flashed onto my Pico and the Micro Pico extension set up in Visual Studio Code, I was ready to dive into coding.
Coding the Python Programm
This code sets up an OLED display and dynamically updates it with various pieces of information. Here’s a breakdown of the code:
import machine
from lib import ssd1306
import time
# Define pins for Raspberry Pi Pico
i2c = machine.I2C(1, scl=machine.Pin(7), sda=machine.Pin(6))
# Define OLED screen resolution (128x64 pixels)
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
# Initialize OLED screen
oled.fill(0)
oled.text("Hello, World!", 0, 0)
oled.show()
# Define frequency variable
frequency = 100 # For example, 100 Hz
# Define signal type variable
signal_type = 'sine wave' # For example, sine
# Define power consumption variable
power = 15 # For example, 15 Watts
# Define time from starting
start_time = time.time()
# Main loop
while True:
# Clear the screen
oled.fill(0)
# Display title and frame
oled.text("HV Gene infos",0,0)
oled.rect(0, 10, 128, 54,1)
# Display variables
oled.text("Type: {}".format(signal_type), 2, 12)
oled.text("Freq: {} Hz".format(frequency), 2, 22)
oled.text("Power: {} Watts".format(power), 2, 32)
oled.text("Timer: {} min".format(round((time.time() - start_time)/60,1)), 2, 42)
# Update the display
oled.show()
# Wait for a short period of time (for example, 1 second)
time.sleep(1)
Result
The result of programming and using the OLED panel that will be used for my final project is shown below. Information is clearly displayed on the OLED screen, and the time is progressing.
Testing with My PCB
I then decided to test the OLED panel with my custom board made last week. This is the board that will be used for my final project (or at least a first version). Since I’ve decided to program in python, I’ll use Thonny to program the Seeed RP2040. The extension used previously with VSCode doesn’t allow me to use the Seeed vesion ofthe RP2040.
The code is exactly the same. The difference is that I have to register the ssd1306 library on the Seeed RP2040 by registering it via Thonny on the microcontroller so that it can access it. The VSCode extension did this automatically on the Raspberry Pi Pico.
The setup is shown below and works in the same way as with the Raspberry Pi Pico.
Measure Power Consumption of my OLED
This part was done in group and is accessible on the group page.
References
Here are a few references I’ve used to help me. I also used ChatGPT.