hero

Scene Changer

Learn how to change the scene on an OLED screen with the press of a button

Please complete the basic installation-chapters before starting a project.

This project will show you how to interact with a screen using a button. The script will follow a sequence when the button is pressed and change what is displayed. It will call different functions depending on the current function that is displayed.

Required Hardware

You will need the following to build this project:

Circuit

Assemble the components according to the circuit diagram below:

Circuit for the scene changer
Circuit for the scene changer

Code

Read the comments in the code and change the variables as necessary then upload it to your board.

In order to use the OLED screen you will need to install the following module:

1mip.install("https://raw.githubusercontent.com/micropython/micropython-lib/master/micropython/drivers/display/ssd1306/ssd1306.py")

If you are unsure how to install external modules you can read up on it here

1from machine import SoftI2C, Pin
2from Button import Button
3import ssd1306_1315 as ssd1306
4import framebuf
5import gc
6
7DISPLAY_WIDTH = 128
8DISPLAY_HEIGHT = 32
9class Point():
10 def __init__(self, x, y):
11 self.x = x
12 self.y = y
13 def set_coords(coords):
14 return
15
16i2cbus = SoftI2C(scl = Pin(12), sda = Pin(11), freq = 100000)
17print(i2cbus)
18oled = ssd1306.SSD1306_I2C(DISPLAY_WIDTH, DISPLAY_HEIGHT, i2cbus)
19
20counter_pressed = 0
21total_pressed = 0
22
23def textDisplay():
24 oled.show()
25 oled.text('Arduino', 40, 0)
26 oled.text('and', 60, 12)
27 oled.text('MicroPython', 23, 24)
28 oled.show()
29
30def arduinoLogo():
31 global total_pressed
32 oled.fill(0)
33 oled.show()
34 total_pressed = total_pressed + counter_pressed
35 oled.text('Number of times', 5, 0)
36 oled.text(f'button was', 5, 10)
37 oled.text(f'pressed: {total_pressed}', 5, 20)
38 oled.show()
39
40def micropythonLogo():
41 oled.fill(0)
42 oled.fill_rect(0, 0, 32, 32, 1)
43 oled.fill_rect(2, 2, 28, 28, 0)
44 oled.vline(9, 8, 22, 1)
45 oled.vline(16, 2, 22, 1)
46 oled.vline(23, 8, 22, 1)
47 oled.fill_rect(26, 24, 2, 4, 1)
48 oled.show()
49
50def button_change(button, event):
51 global counter_pressed
52 if event == Button.PRESSED:
53 counter_pressed += 1
54 if counter_pressed == 1:
55 textDisplay()
56 if counter_pressed == 2:
57 arduinoLogo()
58 if counter_pressed == 3:
59 micropythonLogo()
60 if counter_pressed > 3:
61 oled.fill(0)
62 oled.show()
63 counter_pressed = 0
64
65button_one = Button(17, False, button_change)
66
67while(1):
68 button_one.update()

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.

Suggest Changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. You can read more on how to contribute in the contribution policy.
The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.