0% found this document useful (0 votes)
135 views

SSD1306 OLED Display Scroll Functions and Draw Shapes Using ESP32

This project involves building a digital thermometer to measure body temperature using an ESP32 microcontroller, MicroPython programming language, and a DS18B20 temperature sensor. The thermometer will help the community by providing a safer alternative to mercury thermometers. It works by sensing the user's body temperature with the sensor and displaying it after a short delay. Drawing shapes and scrolling text on an OLED display are also demonstrated using the ESP32 and MicroPython.

Uploaded by

Play List
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
135 views

SSD1306 OLED Display Scroll Functions and Draw Shapes Using ESP32

This project involves building a digital thermometer to measure body temperature using an ESP32 microcontroller, MicroPython programming language, and a DS18B20 temperature sensor. The thermometer will help the community by providing a safer alternative to mercury thermometers. It works by sensing the user's body temperature with the sensor and displaying it after a short delay. Drawing shapes and scrolling text on an OLED display are also demonstrated using the ESP32 and MicroPython.

Uploaded by

Play List
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Abstract

This project is the application and utilization of ESP32 of the e-Gizmo


Micropython Kit. As people worried about our own welfare, as well as the welfare of
others, my teammates and I decided to create a program that would measure body
temperatures. This benefits not only us, but the society as well.

This project uses MicroPython as the programming language. This language


will help us code our digital thermometer that will be used to measure body
temperature. The DS18B20 Temperature Sensor will be sensing the body
temperature. Once the sensor detects the temperature, it will have a delay (in
accordance to the given code below) into sensing how much temperature the body
is.

Once this project has been successfully implemented, we hopefully see to it


that this would be a huge help to the community and to the society that are still using
old mercury thermometers, as mercury is highly poisonous and is harmful to not only
to the environment, but to humans as well.

Project Components

● ESP 32
● DS18B20 Temperature Sensor
● 4.7 kΩ Resistor
● Jumper Wires
● Breadboard
Methodology

The DS18B20 temperature sensor can be powered through a VDD pin


(normal mode) or from the data line (parasite mode). You can choose either mode
like so

Parasite Mode Normal Mode

Projected Algorithm
Abstract
This application will show a function on how to control an OLED Display with
MicroPython using ESP32. This SSD1306 OLED Display Scroll Functions and Draw
Shapes using ESP32 can scroll the entire screen horizontally and vertically and it
can also draw various shapes, like rectangle, triangle, circle and line.
The acronym “OLED” stands for Organic Light-Emitting Diode, a technology
that uses LEDs in which the light is produced by organic molecules. OLEDs enable
emissive displays - which means that each pixel is controlled individually and emits
its own light (unlike LCDs in which the light comes from a backlighting unit).
An OLED display have the following advantage: Improved image quality -
better contrast, higher brightness, fuller viewing angle, a wider color range and much
faster refresh rates; Lower power consumption; and Simpler design that enables
ultra-thin, flexible, foldable and transparent displays.

Components:
The materials required for this project are the following:
 ESP32
 0.96 inch OLED Display
 Breadboard
 Jumper wires

Methodology:
Before we start, you need to install MicroPython firmware in your ESP32. You also
need to install IDE to write and upload the code to your board. We suggest using
Thonny IDE or uPyCraft IDE.
For an ESP32 Board, follow this schematic diagram:

SSD1306 OLED Library


The library to write to the OLED display isn’t part of the standard MicroPython library
by default. So, we need to upload the library to our ESP32/ESP8266 board.
Link: https://how2electronics.com/micropython-interfacing-oled-display-esp32/
#Video_Tutorial_038_Guide

MicroPython OLED Scroll Functions

The ss1306.py library comes with a scroll(x, y) function. It scroll x number of


pixels to the right and y number of pixels down.

Scroll OLED Screen Horizontally


Sometimes you want to display different screens on the OLED display. For example,
the first screen shows sensor readings, and the second screen shows GPIO states.

Scroll in horizontally
The following function scroll_in_screen(screen) scrolls the content of an
entire screen (right to left).

def scroll_in_screen(screen):
for i in range (0, oled_width+1, 4):
for line in screen:
oled.text(line[2], -oled_width+i, line[1])
oled.show()
if i!= oled_width:
oled.fill(0)
This function accepts as argument a list of lists. For example:

screen1 = [[0, 0 , screen1_row1], [0, 16, screen1_row2], [0,


32, screen1_row3]]

Each list of the list contains the x coordinate, the y coordinate and the message [x,
y, message].

As an example, we’ll display three rows on the first screen with the following
messages.

screen1_row1 = "Screen 1, row 1"


screen1_row2 = "Screen 1, row 2"
screen1_row3 = "Screen 1, row 3"

Then, to make your screen scrolling from left to right, you just need to call
the scroll_in_screen() function and pass as argument the list of lists:

scroll_in_screen(screen1)

Scroll out horizontally


To make the screen scroll out, you can use
the scroll_out_screen(speed) function that scrolls the entire screen out of the
OLED. It accepts as argument a number that controls the scrolling speed. The speed
must be a divisor of 128 (oled_width)
def scroll_out_screen(speed):
for i in range ((oled_width+1)/speed):
for j in range (oled_height):
oled.pixel(i, j, 0)
oled.scroll(speed,0)
oled.show()

Now, you can use both functions to scroll between screens. For example:

scroll_in_screen(screen1)
scroll_out_screen(4)
scroll_in_screen(screen2)
scroll_out_screen(4)

Continuous horizontal scroll


If you want to scroll the screen in and out continuously, you can use
the scroll_screen_in_out(screen) function instead.

def scroll_screen_in_out(screen):
for i in range (0, (oled_width+1)*2, 1):
for line in screen:
oled.text(line[2], -oled_width+i, line[1])
oled.show()
if i!= oled_width:
oled.fill(0)

You can use this function to scroll between screens, or to scroll the same screen
over and over again.

scroll_screen_in_out(screen1)
scroll_screen_in_out(screen2)
scroll_screen_in_out(screen3)

Scroll OLED Screen Vertically


We also created similar functions to scroll the screen vertically.

Scroll in vertically
The scroll_in_screen_v(screen) scrolls in the content of the entire screen.

def scroll_in_screen_v(screen):
for i in range (0, (oled_height+1), 1):
for line in screen:
oled.text(line[2], line[0], -oled_height+i+line[1])
oled.show()
if i!= oled_height:
oled.fill(0)

Scroll out vertically


You can use the scroll_out_screen_v(speed) function to scroll out the screen
vertically. Similarly to the horizontal function, it accepts as argument, the scrolling
speed that must be a number divisor of 64 (oled_height).

def scroll_out_screen_v(speed):
for i in range ((oled_height+1)/speed):
for j in range (oled_width):
oled.pixel(j, i, 0)
oled.scroll(0,speed)
oled.show()

Continuous vertical scroll


If you want to scroll the screen in and out vertically continuously, you can use
the scroll_in_out_screen_v(screen) function.

def scroll_screen_in_out_v(screen):
for i in range (0, (oled_height*2+1), 1):
for line in screen:
oled.text(line[2], line[0], -oled_height+i+line[1])
oled.show()
if i!= oled_height:
oled.fill(0)

MicroPython OLED Draw Shapes


To draw shapes on the OLED display using MicroPython we’ll use the Adafruit_GFX
MicroPython Library.

Adafruit GFX Library


To draw shapes on the OLED display, we’ll use the Adafruit GFX Library. This library
isn’t part of the standard MicroPython library by default. So, you need to upload the
library to your ESP32 board.

Follow the previous instructions on how to install a library, but for the GFX library.
Save the GFX library file as gfx.py. Then, you can use the library functionalities by
importing the library in your code.
Link: https://randomnerdtutorials.com/micropython-ssd1306-oled-scroll-shapes-
esp32-esp8266/

Methodology

In summary, here’s how to draw shapes. First, you need to include


the ssd1306 and gfx libraries as well as the Pin and SoftI2C modules.

from machine import Pin, SoftI2C


import ssd1306
from time import sleep
import gfx

Then, define the pins for the ESP32.

i2c = SoftI2C(scl=Pin(22), sda=Pin(21))

We’re using a 128×64 OLED display. If you’re using an OLED display with different
dimensions, change that on the following lines:

oled_width = 128
oled_height = 64

Create an ss1306 object called oled.

oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)

Then, we need to create a gfx object to draw shapes. In this case, it’s


called graphics. It takes as arguments, the width and height of the drawing area. In
this case, we want to draw in the entire OLED, so we pass the OLED width and
height. We should also pass as argument one function of our display that draws
pixels, in our case is oled.pixel.

graphics = gfx.GFX(oled_width, oled_height, oled.pixel)

Then, you can use the drawing functions we’ll show you next to display shapes.

You might also like