Smart Monitoring System
Smart Monitoring System
So that you can carry out the installations shown in this tutorial you should have
downloaded the Thonny programming environment on your device. Also, you need to
have installed the firmware of MicroPython on your Raspberry Pi Pico. The extended
modifications (see p 42 in manual) including the extra components and their connectivity
must also be made on the breadboard.
Electrical Hardware
1 x Raspberry Pi Pico
1 x Full-size breadboard
1 x Micro-USB cable
Jumper wires as needed
1 x OLED SSD1306 display
5 x 220 Ohm resistor
2 x 1k Ohm resistor
1 x LDR photoresistor
This project has been funded with support from the European Commission. This publication [communication] 1
reflects the views only of the author, and the Commission cannot be held responsible for any use which may be
made of the information contained therein.
2021-1-DE02-KA220-ADU-000033587
As regards physical skills you should be able to count off holes on the breadbord and
insert components to it.
2. Learning content
2.1 Theoretical background
What is an OLED SSD1306 display?
An OLED SSD1306 display is a type of electronic display that uses OLED (Organic Light-
Emitting Diode) technology and is controlled by an SSD1306 display driver IC (Integrated
Circuit). OLED is a display technology that uses organic compounds to emit light when an
electric current is applied. Unlike traditional LCD displays, OLEDs do not require a
backlight, which allows them to be thinner, lighter, and more power-efficient. OLED
displays offer vibrant colors, high contrast ratios, and fast response times, making them
This project has been funded with support from the European Commission. This publication [communication] 2
reflects the views only of the author, and the Commission cannot be held responsible for any use which may be
made of the information contained therein.
2021-1-DE02-KA220-ADU-000033587
suitable for various applications, including smartphones, TVs, and small screens like those
found in wearables and IoT devices. The SSD1306 is an OLED display driver IC
(Integrated Circuit) manufactured by Solomon Systech. This driver IC is designed to
control OLED displays and is commonly used for small monochrome OLED screens. It
communicates with a microcontroller or other control circuitry to display text, graphics, and
images on the OLED screen. The SSD1306 IC is compatible with various microcontrollers
and development platforms, making it a popular choice for hobbyists and developers.
OLED SSD1306 displays are often used in DIY electronics projects, embedded systems,
and wearable devices because of their compact size, low power consumption, and sharp
image quality. These displays are available in various sizes, resolutions, and
configurations, and they are commonly used in applications like smartwatches, fitness
trackers, digital cameras, and IoT devices where space and power efficiency are important
considerations.
Developers can program the SSD1306 display to show text, graphics, and even simple
animations, making it a versatile choice for adding a visual interface to electronic projects.
Libraries and libraries exist to facilitate interfacing with these displays, making it relatively
easy to integrate them into different microcontroller-based projects.
To use the OLED SSD1306 display you will need to install the necessary libraries to your
Raspberry Pi Pico. Initial information is provided on page of the SmartHome4Seniors
manual. However, additional information is provided below in section 2 of this tutorial.
This project has been funded with support from the European Commission. This publication [communication] 3
reflects the views only of the author, and the Commission cannot be held responsible for any use which may be
made of the information contained therein.
2021-1-DE02-KA220-ADU-000033587
This project has been funded with support from the European Commission. This publication [communication] 4
reflects the views only of the author, and the Commission cannot be held responsible for any use which may be
made of the information contained therein.
2021-1-DE02-KA220-ADU-000033587
This project has been funded with support from the European Commission. This publication [communication] 5
reflects the views only of the author, and the Commission cannot be held responsible for any use which may be
made of the information contained therein.
2021-1-DE02-KA220-ADU-000033587
Open Thonny Python, then go to File Save as…, choose Raspberry Pi Pico, and save
your file under the name main.py. You should have by now all electronics and sensors
mounted to the SmartHome model and connected to the breadboard.
To connect the OLED display you will need 4 male-to-female jumper wires, and to mount
it on the SmartHome model you will need 4 M2 metal bolts and 4 M2 metal nuts.
This project has been funded with support from the European Commission. This publication [communication] 6
reflects the views only of the author, and the Commission cannot be held responsible for any use which may be
made of the information contained therein.
2021-1-DE02-KA220-ADU-000033587
This project has been funded with support from the European Commission. This publication [communication] 7
reflects the views only of the author, and the Commission cannot be held responsible for any use which may be
made of the information contained therein.
2021-1-DE02-KA220-ADU-000033587
PIN_GARAGE = 1
PIN_RED = 3
PIN_YELLOW = 4
PIN_GREEN = 5
PIN_BUZZER = 6
PIN_BUTTON = 7
PIN_PIR = 22
PIN_LED_1 = 10
PIN_LED_2 = 11
PIN_GAS = 12
PIN_RAINDROP = 13
PIN_LDR = 26
PIN_POT = 27
PIN_DHT = 8
PIN_FAN = 9
PIN_FLAME_SENSOR = 2
PIN_SDA = 17
PIN_SCK = 18
PIN_MOSI = 19
PIN_MISO = 16
PIN_RST = 28
PIN_SDA_OLED = 14
PIN_SCK_OLED = 15
#Setup button
button = Pin(PIN_BUTTON, Pin.IN, Pin.PULL_DOWN)
#Setup buzzer
buzzer = Pin(PIN_BUZZER, Pin.OUT)
buzzer.value(0)
led_1.freq(1000)
This project has been funded with support from the European Commission. This publication [communication] 8
reflects the views only of the author, and the Commission cannot be held responsible for any use which may be
made of the information contained therein.
2021-1-DE02-KA220-ADU-000033587
led_2.freq(1000)
led_1.duty_u16(MIN)
led_2.duty_u16(MIN)
sleep(5)
#Setup DC fan
fan = Pin(PIN_FAN, Pin.OUT)
This project has been funded with support from the European Commission. This publication [communication] 9
reflects the views only of the author, and the Commission cannot be held responsible for any use which may be
made of the information contained therein.
2021-1-DE02-KA220-ADU-000033587
while True:
#Tutorial 2 - Garage Door
distance = ultrasonic.distance_cm()
if distance > 20:
ledred.value(1)
ledyellow.value(0)
ledgreen.value(0)
garage_door.write(145)
sleep(0.1)
elif distance > 10:
ledred.value(0)
ledyellow.value(1)
ledgreen.value(0)
garage_door.write(145)
sleep(0.1)
elif distance > 5:
ledred.value(0)
ledyellow.value(0)
ledgreen.value(1)
garage_door.write(200)
sleep(0.1)
RFID.init()
(stat, tag_type) = RFID.request(RFID.REQIDL)
This project has been funded with support from the European Commission. This publication [communication] 10
reflects the views only of the author, and the Commission cannot be held responsible for any use which may be
made of the information contained therein.
2021-1-DE02-KA220-ADU-000033587
if stat == RFID.OK:
(stat, uid) = RFID.SelectTagSN()
if stat == RFID.OK:
fob = int.from_bytes(bytes(uid),"little",False)
#Tutorial 4 - Doorbell
if button.value():
oled.fill(0)
oled.text("Someone's at the door!", 0, 0)
buzzer.value(1)
sleep(0.5)
buzzer.value(0)
sleep(1)
buzzer.value(1)
sleep(1.5)
buzzer.value(0)
sleep(2)
if PIR.value():
led_1.duty_u16(MAX)
led_2.duty_u16(MAX)
sleep(3)
LDR_value = LDR.read_u16()
print (LDR_value)
if LDR_value > LDR_THRESHOLD:
led_1.duty_u16(MAX)
led_2.duty_u16(MAX)
sleep(0.1)
This project has been funded with support from the European Commission. This publication [communication] 11
reflects the views only of the author, and the Commission cannot be held responsible for any use which may be
made of the information contained therein.
2021-1-DE02-KA220-ADU-000033587
This project has been funded with support from the European Commission. This publication [communication] 12
reflects the views only of the author, and the Commission cannot be held responsible for any use which may be
made of the information contained therein.
2021-1-DE02-KA220-ADU-000033587
if gas_sensor() == 0:
oled.text("Gas Detected!", 0, 40)
print("Gas Detected")
#Activate the buzzer when gas is detected
buzzer.value(1)
sleep(0.5)
#Turn off the buzzer after the delay
buzzer.value(0)
else:
print("Gas Not Detected")
sleep(1.5)
This project has been funded with support from the European Commission. This publication [communication] 13
reflects the views only of the author, and the Commission cannot be held responsible for any use which may be
made of the information contained therein.
2021-1-DE02-KA220-ADU-000033587
sleep(0.5)
buzzer.value(0)
sleep(0.5)
sleep(1)
oled.show()
3. Summary
In this tutorial you have learned how you can connect and program an OLED display on
your house model to display information about the connected electronics and sensors.
This tutorial utilized all sensors and electronics provided in the SmartHome4SENIORS Kit
in order to simulate a fully functional smart home.
Self-assessment
1. Can an OLED SSD1306 display show information of inputs, such as notifying the
user of a doorbell being pressed?
a. Yes
b. No
c. It depends on the setup.
2. Why do you need to install a separate MicroPython library before using the OLED
SSD1306 display?
a. The library is required to be installed because communication with the
Raspberry Pi Pico can be done without it.
b. The library creates the connections between your computer and the
Raspberry Pi Pico, which could not be done otherwise.
c. The library makes the interface between the display and the Raspberry Pi
Pico easier, improving reliability and reducing coding time.
References
n/a
This project has been funded with support from the European Commission. This publication [communication] 14
reflects the views only of the author, and the Commission cannot be held responsible for any use which may be
made of the information contained therein.
2021-1-DE02-KA220-ADU-000033587
This project has been funded with support from the European Commission. This publication [communication] 15
reflects the views only of the author, and the Commission cannot be held responsible for any use which may be
made of the information contained therein.
2021-1-DE02-KA220-ADU-000033587
Appendix
ssd1306.py
# MicroPython SSD1306 OLED driver, I2C and SPI interfaces
# register definitions
SET_CONTRAST = const(0x81)
SET_ENTIRE_ON = const(0xA4)
SET_NORM_INV = const(0xA6)
SET_DISP = const(0xAE)
SET_MEM_ADDR = const(0x20)
SET_COL_ADDR = const(0x21)
SET_PAGE_ADDR = const(0x22)
SET_DISP_START_LINE = const(0x40)
SET_SEG_REMAP = const(0xA0)
SET_MUX_RATIO = const(0xA8)
SET_COM_OUT_DIR = const(0xC0)
SET_DISP_OFFSET = const(0xD3)
SET_COM_PIN_CFG = const(0xDA)
SET_DISP_CLK_DIV = const(0xD5)
SET_PRECHARGE = const(0xD9)
SET_VCOM_DESEL = const(0xDB)
SET_CHARGE_PUMP = const(0x8D)
def init_display(self):
for cmd in (
SET_DISP | 0x00, # off
# address setting
SET_MEM_ADDR,
0x00, # horizontal
# resolution and layout
This project has been funded with support from the European Commission. This publication [communication] 16
reflects the views only of the author, and the Commission cannot be held responsible for any use which may be
made of the information contained therein.
2021-1-DE02-KA220-ADU-000033587
SET_DISP_START_LINE | 0x00,
SET_SEG_REMAP | 0x01, # column addr 127 mapped to SEG0
SET_MUX_RATIO,
self.height - 1,
SET_COM_OUT_DIR | 0x08, # scan from COM[N] to COM0
SET_DISP_OFFSET,
0x00,
SET_COM_PIN_CFG,
0x02 if self.width > 2 * self.height else 0x12,
# timing and driving scheme
SET_DISP_CLK_DIV,
0x80,
SET_PRECHARGE,
0x22 if self.external_vcc else 0xF1,
SET_VCOM_DESEL,
0x30, # 0.83*Vcc
# display
SET_CONTRAST,
0xFF, # maximum
SET_ENTIRE_ON, # output follows RAM contents
SET_NORM_INV, # not inverted
# charge pump
SET_CHARGE_PUMP,
0x10 if self.external_vcc else 0x14,
SET_DISP | 0x01,
): # on
self.write_cmd(cmd)
self.fill(0)
self.show()
def poweroff(self):
self.write_cmd(SET_DISP | 0x00)
def poweron(self):
self.write_cmd(SET_DISP | 0x01)
def show(self):
x0 = 0
x1 = self.width - 1
if self.width == 64:
# displays with width of 64 pixels are shifted by 32
x0 += 32
x1 += 32
self.write_cmd(SET_COL_ADDR)
This project has been funded with support from the European Commission. This publication [communication] 17
reflects the views only of the author, and the Commission cannot be held responsible for any use which may be
made of the information contained therein.
2021-1-DE02-KA220-ADU-000033587
self.write_cmd(x0)
self.write_cmd(x1)
self.write_cmd(SET_PAGE_ADDR)
self.write_cmd(0)
self.write_cmd(self.pages - 1)
self.write_data(self.buffer)
class SSD1306_I2C(SSD1306):
def __init__(self, width, height, i2c, addr=0x3C,
external_vcc=False):
self.i2c = i2c
self.addr = addr
self.temp = bytearray(2)
self.write_list = [b"\x40", None] # Co=0, D/C#=1
super().__init__(width, height, external_vcc)
class SSD1306_SPI(SSD1306):
def __init__(self, width, height, spi, dc, res, cs,
external_vcc=False):
self.rate = 10 * 1024 * 1024
dc.init(dc.OUT, value=0)
res.init(res.OUT, value=0)
cs.init(cs.OUT, value=1)
self.spi = spi
self.dc = dc
self.res = res
self.cs = cs
import time
self.res(1)
time.sleep_ms(1)
self.res(0)
time.sleep_ms(10)
self.res(1)
super().__init__(width, height, external_vcc)
This project has been funded with support from the European Commission. This publication [communication] 18
reflects the views only of the author, and the Commission cannot be held responsible for any use which may be
made of the information contained therein.
2021-1-DE02-KA220-ADU-000033587
self.cs(0)
self.spi.write(bytearray([cmd]))
self.cs(1)
This project has been funded with support from the European Commission. This publication [communication] 19
reflects the views only of the author, and the Commission cannot be held responsible for any use which may be
made of the information contained therein.