0% found this document useful (0 votes)
148 views10 pages

Raspberry Pi Zero 2W - How To Enable I2C and OLED Display Example

Uploaded by

S Kj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
148 views10 pages

Raspberry Pi Zero 2W - How To Enable I2C and OLED Display Example

Uploaded by

S Kj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

robu.

in

Raspberry Pi Zero 2W : How to


enable I2C and OLED display
example - Robu.in | Indian Online
Store | RC Hobby | Robotics
MACFOS

8–10 minutes

In this guide, we’ll see how to enable I2C on Raspberry Pi Zero


2W, and attach a SSD 1306 OLED display and display few things
on it. So, let’s get

In this guide, we'll see how to enable I2C on Raspberry Pi Zero


2W, and attach a SSD 1306 OLED display and display few things
on it.

So, let's get started

• What is Raspberry Pi ?

• Pi Zero 2W
• Features

• Hardware Requirements of this Project

• Software Requirements of this Project

• Circuit Diagram and Hardware Interfacing


• Circuit Diagram
• Hardware Interfacing
• Raspberry Pi Zero 2W

• Prerequisites

• Coding

• Conclusion

What is Raspberry Pi ?

It's a small, affordable, single-board computer developed by the


Raspberry Pi Foundation. Designed initially for educational
purposes, it provides a platform for learning programming,
computer science, and even in few industries. Over time, it has
gained popularity among hobbyists and professionals for its
versatility in various projects, such as home automation, media
centers, robotics, and more.

The Raspberry Pi runs a variety of operating systems, most


notably Raspberry Pi OS (formerly Raspbian), which is based on
Debian Linux. It supports programming languages like Python,
C, C++, Java, and others, making it an ideal tool for both
beginners and advanced users.

The device features USB ports, HDMI output, GPIO pins for
hardware projects, and network connectivity, making it a
powerful and flexible tool for numerous applications.

The Raspberry Pi Zero 2W is a compact and cost-effective


variant of the Raspberry Pi series, designed to offer significant
performance improvements over its predecessor, the Raspberry
Pi Zero W.
Released by the Raspberry Pi Foundation, the Pi Zero 2W
retains the small form factor of the original Pi Zero models but
its new features and enhanced specifications, making it more
capable for various projects.

Features

• A quad-core 64-bit ARM Cortex-A53 CPU, which provides a


significant performance boost compared to the single-core CPU
of the original Pi Zero.

• 512MB of LPDDR2 SDRAM, sufficient for lightweight


computing tasks and various IoT applications.

• Integrated 2.4GHz IEEE 802.11b/g/n wireless LAN and


Bluetooth 4.2, offering robust wireless communication
capabilities.

• CSI-2 camera connector, allowing the attachment of Raspberry


Pi camera modules.

• Mini HDMI port for video output.

The Raspberry Pi Zero 2W is designed to be a low-cost, low-


power solution suitable for projects that require a small form
factor and modest computing power. Its improved performance
and connectivity make it ideal for embedded applications, DIY
electronics, portable projects, and educational purposes.

Hardware Requirements of this Project

• Raspberry Pi Zero 2W

• Pi Zero/ Zero 2W Case


• 32GB Micro SD Card

• Heat Sinks

• Cables

• SSD 1306 Display

Software Requirements of this Project

• Python Packages and few other dependencies

Circuit Diagram and Hardware Interfacing

Circuit Diagram

Hardware Interfacing

Raspberry Pi Zero 2W

Raspberry Pi Zero 2W SSD 1306 OLED

+3.3V VCC
GND GND

GPIO 2 (SDA) SDA

GPIO 3 (SCL) SCL

Prerequisites

Before we diving right into coding, we need to do few things


first. To get all the libraries and dependencies sorted. And also
we need to enable I2C if it isn't on in the first place.

To turn on the I2C, firstly we need to head into configuration


menu by -
sudo raspi-config
And head into the interface options -> select I2C and enable it
from there.

optional - we could increase the I2C bus speed from default


100000 to 400000 to make animations or video playback much
smoother.
To do that we need to edit the config file, and add extra
parameter to it.
Firstly head into config file by -
sudo nano /boot/firmware/config.txt
and goto line where it says - dtparam=12c_arm=on and
change that line to
dtparam=i2c_arm=on, i2c_arm_baudrate=400000
and save and exit the config file by ctrl+s and ctrl+x
respectively.
To check if your I2C is present or not, this the command -
ls -l /dev/i2c*
And you should see two I2C controllers present which are 1 and
2 respectively.

Next we need to install few libraries, few of which could be there


already. But just to make sure, run all the commands once -
sudo apt install -y python3-dev
sudo apt install -y python3-smbus i2c-tools
sudo apt install -y python3-pil
sudo apt install -y python3-setuptools
sudo apt install -y python3-venv

Now, connect the OLED display as per the circuit diagram, and
run the following command to see if your Raspberry Pi can see
the display or not -
i2cdetect -y 1

You can cross verify it by looking at the back of the OLED


display and see a resistor shorting 0x3C (0x78).
If your resistor is shorting 0x7A then the address reported
should be 0x3D.

Now that we have setup all the necessary things by which we'll
be able to communicate to the I2C display. Now lets move onto
necessary libraries through which we can get something printed
onto the display.

Lets make a project folder and move into it using the respective
commands-
mkdir oled_display
cd oled_display

We'll be creating a virtual environment for this entire thing


inside this folder. Why? It is a good practice to to contain all the
libraries and dependencies inside a folder, protecting the core
python installation of the Pi. And in case something goes wrong,
all we need to do is to simply delete the folder and make it again.

To create a virtual environment, we'll be using -


python3 -m venv [folder name you want to give
without the brackets]
and to start the virtual environment, use the command -
source [folder name you gave without the brackets]/
bin/activate

Now you'll be in a safe environment, to do experiments without


the worry of bricking the Raspberry Pi or the python installation
in it.

To control or communicate with OLED, we need luma.oled


library. Get it by running the command -
python -m pip install --upgrade luma.oled

After that we need git, to copy the examples from a github repo.
Get git by using this -
sudo apt install git -y
and followed by the cloning of the library to local from github by
-
git clone https://github.com/rm-hull/
luma.examples.git

Now the dependencies which we defiantly need to run some of


the examples without any errors -
sudo apt install -y python3-pil libjpeg-dev zlib1g-dev
python3-av
sudo apt install -y libfreetype6-dev liblcms2-dev
libopenjp2-7 -y

After done installing these, you can make your own python
program or head into luma.examples/examples to try out pre-
written programs on the OLED display by using command -
cd luma.examples/examples and then
python [the file name you want to try].py

And for any reason if virtual environment gets corrupted beyond


repair, you can always delete the folder using -
rm -rf ~/oled_display/venv

Coding

Copy the following code after opening up the text editor, by


command
sudo nano disp.py and save and exit the text editor
by ctrl+s and ctrl+x.

from luma.core.interface.serial import i2c


from luma.core.render import canvas
from luma.oled.device import ssd1306
from PIL import ImageFont
import time

def main():
# Create I2C interface
serial = i2c(port=1, address=0x3C)

# Create device
device = ssd1306(serial)

# Use a truetype font or the default one if


not available
try:
font =
ImageFont.truetype("arial.ttf", 16)
except IOError:
font = ImageFont.load_default()

# Draw "Hello World" on the OLED display


while True:
with canvas(device) as draw:
draw.text((10, 10), "Hello
World", font=font, fill=255)
time.sleep(1)

if __name__ == "__main__":
main()
After saving the code you can run it by using command -
python disp.py

Conclusion

Hence, after all this setting up and example. You'll have basic
knowledge on how to setup and work with OLED display in
Raspberry PI Zero 2W. If there is any problem or error's arising,
feel free to refer the video which is present on youtube
practically walking you through the process step by step.

- For more such Arduino and Raspberry Pi based projects,


check out our Arduino Playlist and Raspberry Pi Playlist

- And for other more interesting projects, check our


main YouTube Channel

You might also like