Skip to main content

·720 words·4 mins

Week 4 - Embedded Programming
#

Hero Shot:
#

Hero Shot
Hero Shot

TL;DR
#

I built a 4-bit binary counter using a Raspberry Pi Pico W. I started by controlling LEDs with bitwise operations to display binary numbers. Then I added a button to increment the count, fixing unstable input using a pull-up resistor and debounce delay. Finally, I connected an OLED via I2C to show the decimal value, troubleshooting initialization errors by checking connections and adding error handling.

Group Assignment
#

Link to this week’s group assignment

Raspberry Pi Pico Datasheet
#

Before starting this week’s project, I read the datasheet for Raspberry Pi Pico.

Raspberry Pi Pico Pinouts
Raspberry Pi Pico Pinouts

Raspberry Pi Pico is an affordable yet powerful board for RP2040 microcontroller. It is versatile and flexible for embedded projects.

  • It uses a dual-core cortex M0+ processor running up to 133 MHz, allowing it to handle multiple tasks efficiently.
  • The chip has 264 KB of SRAM for fast data access and uses external flash memory to store programs.
  • It provides 30 GPIO pins, which can be used for digital input/output, and 4 of them also support analog input (ADC). All GPIO operates at 3.3V, so components must be compatible with this voltage.
  • The microcontroller includes several communication interfaces such as:
    • I2C
    • SPI
    • UART
    • PWM outputs for controlling LEDs or motors
  • It also has a 12-bit ADC for reading analog signals and a USB interface for programming and communication with a computer.

Programming Raspberry Pi Pico
#

For this week’s project, I used a Raspberry Pi Pico W microcontroller board. As for my approach, I first simulated the system before assembling the physical components.

Simulation
#

I began the project using Wokwi to simulate the circuit and test the code before building the physical system. Wokwi is an online simulation platform where you can simulate Raspberry Pi Pico, ESP32, Arduino, and STM32 circuits. It has a built-in IDE.

First, I connected four LEDs to represent a 4-bit binary number and wrote a function to display numbers in binary using bitwise operations (& and >>). This allowed me to verify that each LED correctly represented a specific bit.

Next, I added a push button in the simulation to increment the counter. During this stage, I implemented basic logic to ensure that each button press increased the value by one. I also tested timing and delays to prevent the counter from increasing too quickly.

After that, I integrated an SSD1306 OLED display using I2C in the simulation. To this, I created a new file called ssd1306.py and asked ChatGPT to provide me with the library for the OLED display. At this stage, I created a function called display_count() to handle both the LED output and the OLED display. This function takes a number as input, displays its binary form on the LEDs, and shows the corresponding decimal value on the OLED. This allowed me to manage both outputs in a single, organized way.

Using Wokwi helped me quickly debug logic errors and confirm that all components worked together before moving to real hardware.

Assembly
#

After confirming that the system worked in simulation, I assembled the circuit using the Raspberry Pi Pico.

Components Used
Components Used

The IDE I used to program the Raspberry Pi Pico is Thonny. To have my program working succesfully, I saved both the ssd1306.py library and main.py file on my Pico.

Saving to Raspberry Pi Pico
Saving to Raspberry Pi Pico - Step 1

Saving to Raspberry Pi Pico
Saving to Raspberry Pi Pico - Step 2

I connected the LEDs to GPIO pins and wired the button to a digital input pin. Initially, I encountered unstable button behavior due to a floating input, which I fixed by enabling an internal pull-up resistor.

I then connected the OLED display using I2C. During this stage, I faced communication errors (OSError: EIO) when initializing the display. To solve this, I checked the wiring, changed the pin connections, confirmed the device address using an I2C scan, adjusted the communication frequency, and added a short delay before initialization.

To make the system more reliable, I added error handling so the program would continue running even if the OLED failed to initialize. Finally, I tested the complete system, ensuring that each button press correctly incremented the counter, updated the LEDs in binary, and displayed the decimal value on the OLED.

Files:

Senanur Düzenli
Author
Senanur Düzenli
Maker of Many Hats | Engineer, Mentor, Educator