Raspberry Pi Pico Project

Display Analog Value of Potentiometer on SSD1306 OLED using Raspberry Pi Pico

Introduction

Potentiometer and Raspberry Pi Pico

Display Analog Value of Potentiometer on SSD1306 OLED using Raspberry Pi Pico- The Raspberry Pi Pico, a microcontroller board based on the RP2040 microcontroller, offers a versatile platform for various electronic projects. Combining it with an SSD1306 OLED display opens up possibilities for real-time data visualization. This article explores how to display the analog voltage values from a potentiometer on an SSD1306 OLED screen using a Raspberry Pi Pico. This project serves as an excellent learning tool for understanding the basics of analog-to-digital conversion, I2C communication, and data display on a screen.




Components Required

Raspberry Pi Pico

SSD1306 OLED display

Potentiometer

Jumper wires

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

About SSD1306 OLED display

The SSD1306 OLED display is a small, self-luminous display module that uses organic light-emitting diodes (OLEDs) to produce bright and colorful images. It is commonly used in small electronic devices such as smartphones, smartwatches, and digital cameras.

Potentiometer and Raspberry Pi Pico

One of the key advantages of the SSD1306 OLED display is its low power consumption. Unlike traditional LCD displays, OLED displays do not require a backlight, which significantly reduces power consumption and extends battery life.



Interfacing SSD1306 Oled and Potentiometer with Pico:

Before we can start displaying the analog value on the OLED, we need to connect the components properly.

Potentiometer and Raspberry Pi Pico

To set up your SSD1306 OLED display with the Raspberry Pi Pico, begin by connecting the VCC pin of the OLED display to the 3.3V pin on the Pico. Then, attach the GND pin of the OLED to the Pico’s GND pin. This establishes the power connection for the display. Next, for data communication, connect the SDA pin of the OLED display to the GP16 pin, and the SCL pin to the GP17 pin on the Raspberry Pi Pico. Now, for the potentiometer setup: connect one terminal to the Pico’s 3.3V pin and the other terminal to the GND pin. Finally, attach the middle terminal of the potentiometer to the GP26 pin on the Pico. This configuration allows the potentiometer to feed analog input to the Pico, which can then be displayed on the OLED screen.



Programming the Raspberry Pi Pico

To program the Raspberry Pi Pico Board, we’ll use MicroPython, a variant of Python tailored for microcontrollers. You have the option to use either Thonny IDE or any other IDE for coding and testing the board.

My preference is for Thonny IDE due to its user-friendly interface. The programming process is divided into two primary components:

ssd1306.py

main.py

The rationale behind this division is that the OLED Display initially requires the SSD1306 Driver Code. Consequently, our first step is to write and upload the code for the SSD1306 Driver. Once the SSD1306 Code is successfully uploaded to the Pico board, we can proceed to execute the main.py script.

SSD1306 oled Display driver code:

In Thonny IDE, start by creating a new file and then paste the ssd1306 OLED driver code into it. Afterward, save this file as ‘ssd1306.py‘. In my previous article, I provided a detailed explanation on how to interface the ssd1306 OLED display with the Raspberry Pi Pico, including various methods for library installation. Therefore, I recommend visiting that article for more in-depth information.

Potentiometer and Raspberry Pi Pico



main.py:

In Thonny IDE, open another new tab. Copy the code provided below and paste it into the thonny IDE window. Once done, save this file with the name ‘main.py‘.

Potentiometer and Raspberry Pi Pico

Once the ssd1306.py and main.py codes are saved, click on the run button or simply press the F5 button on the keyboard.

Potentiometer and Raspberry Pi Pico




After clicking the run button The SSD1306 OLED Display will start showing the Analog voltage value from the potentiometer.

Potentiometer and Raspberry Pi Pico

By rotating the potentiometer, the voltage value will change.

Potentiometer and Raspberry Pi Pico



Code Explanation:

This Raspberry Pi Pico code is designed for interfacing with an OLED display and a potentiometer, demonstrating both digital and analog input/output capabilities. The script begins by importing necessary modules and classes: Pin, I2C, ADC, and Timer from the machine module for hardware interfacing, and SSD1306_I2C from the ssd1306 module for controlling the OLED display. Additionally, utime is imported for handling time-related functions, although it’s not explicitly used in this snippet.

The OLED display dimensions are set with WIDTH and HEIGHT variables, corresponding to a standard 128×64 pixel SSD1306 OLED display. The I2C communication interface is initialized using I2C(0, scl=Pin(17), sda=Pin(16), freq=200000), specifying the use of GPIO pins 17 and 16 for the serial clock and data lines, and setting a communication frequency of 200,000 Hz. An oled object is created to interact with the OLED display over this I2C interface.

For analog input, an Analog-to-Digital Converter (ADC) is initialized on GPIO pin 26 using ADC(26). This ADC is used to read the value from a potentiometer. A conversion_factor is defined to convert the raw ADC readings into a voltage value, based on the Pi Pico’s reference voltage of 3.3V.

In the main loop of the code, an infinite loop is created with while True: Inside this loop, the potentiometer’s value is read as a 16-bit unsigned integer using pot_Val.read_u16(). This value is then multiplied by the conversion_factor to obtain the voltage level. The OLED display is first cleared using oled.fill(0), and then the text “Voltage: ” followed by the calculated voltage value, rounded to one decimal place, is displayed on the screen at specific coordinates. The oled.show() function updates the OLED display with this new information.

Overall, this code demonstrates how the Raspberry Pi Pico can be used for reading analog sensor data, processing it, and displaying the results on an OLED screen, making it a useful tool for a wide range of electronic and IoT projects.



Conclusion

In conclusion, displaying analog values on an SSD1306 OLED using a Raspberry Pi Pico is a rewarding and versatile project that combines the power of the Raspberry Pi Pico’s GPIO pins with the visual appeal of the OLED display. By interfacing the Pico with analog sensors and leveraging Python programming, you can create dynamic and informative displays for various applications such as environmental monitoring, home automation, or data visualization.

Throughout this project, you’ve learned how to establish communication with the SSD1306 OLED display, read analog sensor data using the Pico’s ADC, and then format and display that data on the OLED screen. This integration not only provides you with real-time data visualization but also opens up possibilities for further customization and expansion, allowing you to create sophisticated projects with ease.

Related Articles:

Interfacing SSD1306 OLED Display with Raspberry Pi Pico

HC-SR04 Ultrasonic Distance Sensor with Raspberry Pi Pico and SSD1306 OLED: Displaying Measurement

 

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button