Panel Cookies
Raspberry Pico i2c OLED
13/02/2021 | Views: 19869 | Arduino | by: ELECTRONOOBS      


i2c Scanner


Let’s start with some i2c. We import from machine the I2C and Pin modules. To start an i2c port we use this line and we specify on which pins we want the SDA and SCL, which in this case I place them on pins 8 and 9. Using the i2c. scan function we can see if we have an i2c device connected. I connect my LCD to pins 8 and 9 for SDA and SCL. I run this code and as you can see, on the monitor I get that I found an i2c address, which in this case is 0x27 and that’s correct.



from machine import Pin, I2C

# Init I2C using pins GP8 & GP9 (default I2C0 pins)
i2c = I2C(0, scl=Pin(9), sda=Pin(8), freq=200000)

# Display device address
print("I2C Address      : "+hex(i2c.scan()[0]).upper())


micropython i2c scanner




Part 2 - SSD1306 Library


Let’s see how to control the OLED display using I2C communication. Is connect GROUND and 3.3V from the PICO and the SDA and SCL pins to the GP 8 and 9. Now in the code, I need to use the SSD 13 06 module so I import that. I run this code and as you can see, I get an error. There is no SSD 13 06 module. And that’s correct. We have to add that to the PI as well.


micropython SSD1306 library

So in Thonny create a new file. Save the file and select save on the Raspberry Pi PICO. Give this file the name ssd1306.py and click save. If I now go to open and select the PICO memory, you will see that we have that file on the raspberry pi memory. Now I go to the online code and copy that code. Paste the code in the ssd1306.py file. Click the save button again. Now go back to the main code and run it again. There you go, now we have no more errors and we can use the SSD 13 06 library.


micropython SSD1306 library




Part 3 - OLED code


In the code I start a new i2c communication on those pins. I start the OLED display with this function from the used library. With the fill function we can set all pixel to be on or off by adding a 0 or a 1. So to clear the entire display I make oled.fill 0. With oled.text function we could sent text. Add the text, the column and the row and finally we add oled.show to send the data to the display. Run the code and there you go, I have ELECTRONOOBS on my display.



# Display Image & text on I2C driven ssd1306 OLED display 
from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
import framebuf
import utime

WIDTH  = 128                                            # oled display width
HEIGHT = 64                                            # oled display height

i2c = I2C(0, scl=Pin(9), sda=Pin(8), freq=200000)       # Init I2C using pins GP8 & GP9 (default I2C0 pins)
print("I2C Address      : "+hex(i2c.scan()[0]).upper()) # Display device address
print("I2C Configuration: "+str(i2c))                   # Display I2C config


oled = SSD1306_I2C(WIDTH, HEIGHT, i2c)                  # Init oled display

# Raspberry Pi logo as 32x32 bytearray
buffer = bytearray(b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00|?\x00\x01\x86@\x80\x01\x01\x80\x80\x01\x11\x88\x80\x01\x05\xa0\x80\x00\x83\xc1\x00\x00C\xe3\x00\x00~\xfc\x00\x00L'\x00\x00\x9c\x11\x00\x00\xbf\xfd\x00\x00\xe1\x87\x00\x01\xc1\x83\x80\x02A\x82@\x02A\x82@\x02\xc1\xc2@\x02\xf6>\xc0\x01\xfc=\x80\x01\x18\x18\x80\x01\x88\x10\x80\x00\x8c!\x00\x00\x87\xf1\x00\x00\x7f\xf6\x00\x008\x1c\x00\x00\x0c \x00\x00\x03\xc0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")

# Load the raspberry pi logo into the framebuffer (the image is 32x32)
fb = framebuf.FrameBuffer(buffer, 32, 32, framebuf.MONO_HLSB)

while True:
    # Clear the oled display in case it has junk on it.
    oled.fill(0)

    # Blit the image from the framebuffer to the oled display
    oled.blit(fb, 96, 0)

    # Add some text
    oled.text("ELECTRONOOBS",5,8)
    oled.text("2021",45,18)
    oled.text("SUBSCRIBE",28,40)

    # Finally update the oled display so the image & text is displayed
    oled.show()
    utime.sleep(1)





Part 4 - OLED + ADC


We could now merge the ADC example with this OLED example. So all I have to do in the code, is to take this ADC read and pass it to string using the SRT declaration. I also add the round function so it won’t print all the decimals only 2. To make it look cooler I also use the invert function so the text will be black. This is the result. I now have the ADC read from the potentiometer on the display. Pretty nice right?



# Display Image & text on I2C driven ssd1306 OLED display 
from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
import framebuf
import machine
import utime

sensor_temp = machine.ADC(28)
conversion_factor = 3.3 / (65535)

WIDTH  = 128                                            # oled display width
HEIGHT = 32                                             # oled display height

i2c = I2C(0, scl=Pin(9), sda=Pin(8), freq=200000)       # Init I2C using pins GP8 & GP9 (default I2C0 pins)
print("I2C Address      : "+hex(i2c.scan()[0]).upper()) # Display device address
print("I2C Configuration: "+str(i2c))                   # Display I2C config


oled = SSD1306_I2C(WIDTH, HEIGHT, i2c)                  # Init oled display

# Raspberry Pi logo as 32x32 bytearray
buffer = bytearray(b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00|?\x00\x01\x86@\x80\x01\x01\x80\x80\x01\x11\x88\x80\x01\x05\xa0\x80\x00\x83\xc1\x00\x00C\xe3\x00\x00~\xfc\x00\x00L'\x00\x00\x9c\x11\x00\x00\xbf\xfd\x00\x00\xe1\x87\x00\x01\xc1\x83\x80\x02A\x82@\x02A\x82@\x02\xc1\xc2@\x02\xf6>\xc0\x01\xfc=\x80\x01\x18\x18\x80\x01\x88\x10\x80\x00\x8c!\x00\x00\x87\xf1\x00\x00\x7f\xf6\x00\x008\x1c\x00\x00\x0c \x00\x00\x03\xc0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")

while True:
    reading = sensor_temp.read_u16() * conversion_factor
    # Load the raspberry pi logo into the framebuffer (the image is 32x32)
    fb = framebuf.FrameBuffer(buffer, 32, 32, framebuf.MONO_HLSB)

    # Clear the oled display in case it has junk on it.
    oled.fill(0)

    # Blit the image from the framebuffer to the oled display
    oled.blit(fb, 96, 0)
    
    oled.invert(True)    
    
    # Add some text
    oled.text("ADC: ",5,8)
    oled.text(str(round(reading,2)),40,8)


    # Finally update the oled display so the image & text is displayed
    oled.show()




micropython SSD1306 library





13/02/2021 | Views: 19869 | Arduino | by: ELECTRONOOBS      












Last tutorials

All about Arduino PWM frequencies
Homemade Baby White Noise Generator
Measure AC RMS with Arduino
10 Stage Coilgun - Version 2
Tesla Coil on PCB

ADVERTISERS



Affiliate Disclosure

ADVERTISERS



PCBWAY PCB service





Curso Arduino Online nivel Intermedio