Build a Bitcoin Ticker with ESP32 and Arduino
In this article, I am going to share how to build a Bitcoin ticker that displays the price of BTC/USD. I use an ESP32 microcontroller programmed in an Arduino environment. The Arduino code will read the Bitcoin price from the CoinDesk API, and it will display the price on the 128x64 OLED module.
Hardware
First, we need the following hardware in order to build this project:
ESP32 development board
0.96" 128x64 OLED SSD1306 module
Jumper cable
Wiring Diagram
The OLED module uses an I2C bus. So, we just need two connections between the ESP32 and OLED module, which are pin 32 for SCL and pin 33 for SDA.
Arduino Code
In order to use the OLED module, we need the library Adafruit_SSD1306
. We can install it from the Arduino library manager.
We need to set up several parts in the setup()
function, as shown in the following list:
Serial communication for debugging. The code to set this up is in line 30.
I2C communication to communicate with the OLED module. The code to set this up is in line 33. In lines 8 and 9, we define the SDA and SCL pins.
OLED module. We instantiate the object in line 15, and then initialize it in lines 36 and 37. The I2C address for this module is
0x3C
, as defined in line 12.Wi-Fi connection to a Wi-Fi access point. In lines 40–50, we need to set up a Wi-Fi connection to an access point that has an internet connection. The access point name and password are defined in lines 18 and 19. You need to change this to yours.
HTTP client. We instantiate an HTTP client and declare requests and response variables in lines 22–25. We are going to use the
currentURL
to get the current price and thehistoryURL
to get yesterday's price.
After we initialize the code, we can build the main loop()
function. This is how it works:
In lines 56–72, we make an HTTP request in order to get the current Bitcoin price.
In lines 75–93, we make an HTTP request in order to get the yesterday's Bitcoin price.
In lines 96–98, we do calculations to get the daily percentage change.
In lines 101–114, we display the Bitcoin information on the OLED module.
We use a print_center()
function that is defined in lines 121–128 to print the text to the OLED module with center alignment.
Result
The following figure shows the result. On the OLED, there are daily percentage changes, the current price itself, and the last updated time.
References
Bitcoin Ticker, https://github.com/AndrewBudziszek/bitcoin-ticker-esp32
Last updated