Eye animation on OLED display with arduino nano
New 2024: there is a color lcd version of this project, Animated eyes with arduino nano on color LCD display
Source code https://github.com/intellar/oled_eye_display
#use this command on command line:
git clone https://github.com/intellar/oled_eye_display
[2023] This is a simple project to give a visual feedback to someone interacting with a device. Here the display shows eyes, that can blink, look up, look down, look amused, annoyed, etc. The arduino renders simple basic forms (rounded squares, triangles) that mimic eyes. The following tutorial video takes you through each steps of described on this page.
For this project, we used an oled display found on amazon, costing 11$, and a canaduino, which is a cost effective alternative to the arduino nano v3, costing 6$. This brings the total cost under 20$cad. And if you go on aliexpress, you can get this for under 5$. No links are published since they will most likely not age well. Both devices are shown here:
Amazon.ca
Aliexpress
The nano I bought comes without the pins attached, so a little MacGyver soldering is required to connect the pins on the arduino board. It is easily done with a fine tip and common soldering iron.
The connection of the arduino to the display is fairly simple, and it is important to note that the display uses power on 3v. So dont plug it on the 5v pin. We followed the pinout suggested by the arduino module adafruit_ssd1306 for the i2c connection,
Arduino - Display
3v3 - vcc
Gnd - gnd
A5 - SCL
A4 - SDA
this results in the wiring shown in the next figure :
Now the fun part begins. You can download the code base here (for free! just say thanks). It is a cleaned and concise example of the library. It is meant to be simple and can easily be adapted for more complexe animation.
https://github.com/intellar/oled_eye_display
There are two folders, one contains the arduino code, and the other contains the python script to communicate with the nano. To program the nano, you must upload the program "control_display.ino", that will handle the drawing and the communication with the pc. I use the program arduino ide to compile and upload. The nano must be plugged in the computer for the programmation.
Currently, there are 8 animations in the program, starting with the closed eyes. To trigger other animations, run the example script in the python folder. Note that in the example, the serial port in COM3, it might not be the case on your computer. To find out which one to use, start the hardware manager on windows.
Quick explanation of the code
Animation
The arduino code is based on the adafruit_ssd1306 and adafruit_gfx library. The idea behind the animations is a function that draw the eyes based on a description contained in the global reference variable : left and right eyes width, height, center, position and interspacing. A call to the function draw_eyes will draw filled round rectangle with the specified caracteristics. With this specific oled display, the reference value of the width and height is 40, with corner radius of 10, and inter spacing of 10. Calling certer_eye with reinitialized all value to these default reference and draw the eyes.
To do an animation, for example, to blink, a loop calls the draw_eyes while decrementing and incrementing the height value. In another example. the sleep animation, the height of both eyes is changed to a very small value so the eyes look like they are closed. The happy eye draws the eye and then draw filled triangled to hide a portion of the rectangle, giving the impression of a smile. Calling display at the end of each iteration create the illusion of movement.
Usb
The arduino main loop, loop() listen on the serial data over usb with the Serial module. We made a simple protocol where command starting with the char "A" followed by a number trigger the animation by the number. These command are sent with a python script. This will allow to use other command on the same arduino to trigger other action, closing a robot gripper for example.