In this article, I'll show you how to use an ESP32 or ESP8266, a TCA9548A I2C multiplexer, two 1.3 inch OLED displays, three VL53L0X ToF sensors and a little handicraft work to breathe some life into a
pumpkin for Halloween. Let's go.
What we need
|1||ESP32 NodeMCU or ESP-32 Dev Kit C V4 or ESP8266 D1 Mini|
|1,3" OLED I2C 128x64 Display|
|1||VL53L0X time-of-flight (ToF) laser distance sensor|
|1||TCA9548A I²C multiplexer|
|1||External voltage source 5V (recommended)|
|1||100 µF capacitor (recommended)|
|PC with Arduino IDE and Internet connection|
|Soldering iron (for the pinheaders)|
|Pumpkin (not quite as big, about 15 to 20 cm diameter)|
|Carving tool (from the kitchen)|
For battery operation:
|1||LiPo battery 3.7 V|
|1||MT3608 Step up Module|
|1||TP4056 Charge Controller Module|
Set up the circuit according to the following diagram:
I added a 100µF_capacitor in parallel to the voltage source, because ESPs are very sensitive to voltage fluctuations. This was a tip from a forum.
The D1 Mini and also some ESP32 have no designated VIN pin. In this case use a constant voltage of 5V on the 5V pin of the microcontroller. Pay attention to the data sheet.
If you use a LiPo battery 3.7V like me, I recommend the MT3608 Step up module and the TP4056 charge controller module, because you have to adjust the voltage. Use a voltmeter and set the correct voltage at the potentiometer (if the voltage does not change, you will have to turn counterclockwise for a very long time). You can easily recharge the battery using the charge regulator. I have also added a switch at the voltage output of the converter to avoid having to disconnect the battery every time.
MAKE SURE NOT TO CONNECT THE EXTERNAL POWER SUPPLY AND THE USB-PORT AT THE SAME TIME! - THERE IS NO BLOCKING DIODE BETWEEN THE TWO CONNECTORS
DO NOT CONNECT AN EXTERNAL VOLTAGE TO THE 3.3V-PIN! - there is a voltage converter on the board, which supplies the appropriate voltage from the 5V at the 3.3V-pin.
THE EXTERNAL VOLTAGE SHOULD NOT EXCEED 5V - contrary to the well known Arduinos, the ESP-boards do not have a voltage converter built in to generate constant 5V at the VIN-pin. So you can't connect 9V batteries directly here.
DO NOT DISPLAY PICTURES PERIODICALLY - it is recommended to change the pixels, as displaying the same images over time may cause burn-in.
The multiplexer board, the ToF-Sensor-Board and also the display board have voltage converters built in, so that 5V or 3.3V can be connected.
Please refer to the data sheets of your components. I do not take any liability for damages due to improper handling.
These are my settings for the respective boards. Important is the setting of the CPU frequency, because with this I could increase the clock speed for the I2C bus. It is also recommended to set Erase Flash to All Contents on the ESP8266.
ESP8266 D1 Mini:
ESP32 NodeMCU Devkit:
The I2C multiplexer
As you have already seen above, the multiplexer board has several connectors:
At VIN and GND the board is supplied with 3,3V or 5V voltage. SDA and SCL are the inputs for the I²C-Bus from the microcontroller. Contrary to the TX/RX pins, they are not cross-connected. SDA to SDA and SCL to SCL (or also SCK). With the RST pin on GND the board is reset. With the pins A0 to A2 the standard I²C address (0x70) can be changed by different combinations to 0x71 to 0x77. A0 is the lowest bit. If the pin is set to HIGH, the address is increased by 1. Setting pin A1 to HIGH increases the address by 2, pin A2 to HIGH by 4.
The I²C components are connected to pins SD0/SC0 to SD7/SC7. It is irrelevant what their addresses are. The microcontroller always addresses only the multiplexer. The multiplexer gets a number from 0 to 7 written into a register via the input of the I²C bus, which switches the data stream to the corresponding output. Similar to the switch of a railroad. The sensor supports a maximum clock frequency of 400 KHz. It is possible to cascade several multiplexers. Thus it is possible to control up to 64 devices with 8 multiplexers.
VL53L0X Time-of-Flight Sensor
More information can be found in the blog post VL53L0X Time-of-Flight (ToF) Laser Distance Sensor.
Here I use three sensors, whose I²C addresses I did not change.
1,3" OLED I2C 128x64 Display
These are displays with the chip SH1106. The I²C addresses cannot be changed, so the use of the multiplexer is recommended. The control is not trivial, so you should use libraries like U8G2 or OneBitDisplay, which you can install via the library management.
The abstract program sequence
When the program is started, a short animation of the eyes should be shown for a first functional test. These disappear again and the program goes into a standby mode. This saves the displays and the bright display does not always disturb in the dark. The surprise effect when someone approaches the pumpkin is also important.
If a person approaches and falls below the specified limit, the pumpkin wakes up slowly and the eyes are displayed slightly open. Depending on which sensor has struck, the eyes look in the appropriate direction (center, left, right). If the person approaches further, the pumpkin is awake and the eyes are open. If the person is too close to the pumpkin, the pumpkin will get scared and the eyes will open further.
If the person removes himself from the close vicinity of the pumpkin, he will look around for another minute. For this purpose, different images of the eyes are displayed randomly. If after this time no one should come closer, the off animation is shown again and the program goes into standby again.
To make it a bit more lively, I implemented a blinking animation. For this the random generator will select times between 4 and 6 seconds after which the closed eyes will be displayed for a very short time.
Pictures for the display
I have created some pictures to represent the eyes. There should be three directions and three distance levels. This is how my pictures look like:
The images must be created in the appropriate image format (here 64x128 pixels) and then converted to XBM format. You can do this on this website. The graphics program GIMP is another possibility. There you export the images in XBM format.
Then you open the files with a text editor, preferably one like Notepad++. You can then copy the data and paste it into the imagedata.cpp at the appropriate place. I have done all this already. My program contains all data.
Cardboard is ideal for a prototype that can be produced quickly. You can quickly see what it will look like. In this way the electronics can be tested.
Finding a good place for the sensors was not that easy. I decided on a kind of pedestal:
Remember that the pumpkin is still wet. I have protected the circuit boards and the displays with cling film to prevent short circuits. You can fix the displays carefully from the inside with small screws.
The microcontroller might not be able to provide the power for LEDs to light the interior. One could attach a resistor LED to the output of the step-up converter behind the switch. This will light up when you switch on the technology.
At the end
Here you can download the complete program. Please copy all three files into the same folder.
It is possible that the microcontroller gets stuck in a boot loop. You can recognize this by the fact that the start animation of the eyes is not displayed. Possible causes or solutions:
- Power source not sufficient
- Wrong board settings during upload (ESP8266: CheckFlashConfig.ino, run ESP32 ResetReason)
- Disconnect and reconnect power after upload
- In loops give background tasks computing time with yield()
As you may see, the image composition of the two displays is slightly delayed in relation to each other. This is due to the clock frequency of the I²C bus of the ESPs and the multiplexer, which unfortunately cannot be accelerated any further. An interior lighting should be added. If the power is still sufficient, you could use the DFPlayer to generate noise.
Have fun with the tinkering.
for AZ-Delivery Blog