ESP32, DS18B20, TM1637 integration: Displaying temperature data

In a previous post I wrote about displaying arbitrary data on a TM1637-based 4 digit LED display, highlighting an ESP-IDF component that I extended to display positive and negative floating point numbers. Now we’re going to put that component to use and display actual data from a DS18B20 temperature sensor.

The {% asset_link DS18B20.pdf “DS18B20” %} temperature sensor operates on the Dallas Semiconductor 1-Wire bus. In this application, we aren’t powering the devices using parasitic power. Instead we’re powering the device from an external supply.

Using the DS18B20 with external power supply.

Since we’re using a 3.3v bus, the pullup resistor on DQ is 2.2KΩ, not 4.7K.

In addition to my modified TM1637 component, the project uses David Antliff’s components for the 1-Wire bus protocol and the DS18B20 digital thermometer. You’ll need to follow the instructions in the project README file to clone the project and recurse the three submodules.

Usage

After cloning the project, you’ll need to configure the GPIO pins in use using make menuconfig. One GPIO is used for the 1-Wire bus and two lines are used for each TM1637 display for a total of 5 GPIO pins. Be careful that you do not use pins that are in use for other purposes on your particular board. For example, I initially use GPIO12 for one of the TM1637 displays, but when connected, was unable to flash the ESP32. It appears that GPIO12 is used as a bootstrapping pin. On the particular board I used for this project, I believe that GPIO12 needs to be pulled low at reset but the TM1637 was not permitting it. Moving do another GPIO solved the problem.^[More about GPIO12 here]

After completing the hardware connections and specifying them in make menuconfig, you can then simply make flash to upload the application to the device.

All of the source code is on github.

References

Interfacing an ESP32 to an MCP23017 GPIO expander

While the ESP32 sports a number of GPIO pins, not all are broken out on every board, meaning that sometimes a GPIO expander is necessary. This project is a simple design to test interfacing the ESP32 to an MCP23017 via the I2C interface.

MCP23017 I2C addressing

There are so many tutorials on the MCP23017 that I won’t delve in depth into how it works, but I’ll point out a few features of the custom MCP23017 component that I’m developing as part of this demonstration project. If you need to get up-to-speed developing applications using I2C within the ESP-IDF environment, this tutorial from Luca Dentella is excellent and concise.

MCP23017 component API

All of my custom components are on github. You can clone or download the MCP23017 component there. Once you’ve installed the MCP23017 component in the components directory of your project, you can follow the preliminaries here to start working with it.

Preliminaries

You will need to include the header file for the component in your project and specify the I2C pins that correspond to your hardware design:

#include "mcp23017.h"

#define I2C_SDA	23	// GPIO_NUM_23
#define I2C_SCL 22	// GPIO_NUM_22

In addition, you will also probably want to create a reference to the mcp23017_t structure on a global scope:

mcp23017_t mcp;

Initialization

Before we can use the component, we have to initialize it. The API provides a function for this purpose mcp23017_err_t mcp23017_init(mcp23017_t *mcp).

mcp.i2c_addr = MCP23017_DEFAULT_ADDR;
mcp.port = I2C_NUM_1;
mcp.sda_pin = I2C_SDA;
mcp.scl_pin = I2C_SCL;

mcp.sda_pullup_en = GPIO_PULLUP_ENABLE;
mcp.scl_pullup_en = GPIO_PULLUP_ENABLE;

esp_err_t ret = mcp23017_init(&mcp);
ESP_ERROR_CHECK(ret);

We simply populate the mcp23017_t struct with the data need to initialize the bus and pass its address to the initialization function, checking the return value to make sure everything initialized without error.

Reading and writing registers

The API provides two functions mcp23017_write_register and mcp23017_read_register to write and read MCP23017 registers respectively. To write to a register, you provide the address of the mcp23017_t struct, a register, a group and the value you are writing. All of the device registers are paired into A and B groups. So when you use one of these two functions you provide a register name and the specific group you’re addressing. For example to set all of the pins in group A to output mode:

mcp23017_write_register(mcp, MCP23017_IODIR, GPIOA, 0x00);

Reading is a little different because the return value of mcp23017_read_register like its write counterpart is still an error/status code. Therefore, we pass the address of the variable into which we’d like to read an eight-bit value.

uint8_t current_value;
mcp23017_err_t ret = mcp23017_read_register(mcp, reg, group, &current_value);

If you want to check out the complete demonstration project, you can find it on github.

References

Using TM1637-based LED displays with ESP32

There are three main types of 4 digit seven segment displays to be found on the market: Bare displays without any driver. These come in a variety of colors and with either decimal points or clock-type display with a colon dividing two sets of two digits. 74HC595-based displays. Usually these displays have two daisy-chained 74HC595 shift registers and rely on the host controller to fill the serial registers and handle the multiplexing.

What was the purpose of that tweet?

The thriving New York Times just published a list of questions being posed by Special Counsel Robert S. Mueller, III. What I found particularly interesting was his question about Trump’s intent behind his tweet on May 12, 2017 in which he said: “James Comey better hope that there are no “tapes” of our conversations before he starts leaking to the press!” The question being posed by Mr. Mueller is insightful because it will require Trump to admit to the office of the Special Counsel that he was deliberately attempting to intimidate a party to a Federal investigation.

Serving sensor data via ESP32

Previously, I wrote about using the ESP32 to read sensor data over I2C from the Si7021 temperature and humidity monitor. Today, I’m going to briefly take you through the process of serving this data via the web. Basic project setup Description The project plan is to connect to WiFi in STA mode, collect temperature and humidity data every 5 seconds from a Si7021 sensor via the I2C bus.

ESP32 reading Si7102 temperature and humidity data via I2C bus

Recently I wrote about reading Si7021 temperature and humidity data using a Raspberry Pi. Now let’s try a completely different platform, the ESP32. This is essentially a project to explore using I2C on the ESP32 platform and to understand the build process. Project layout Since we’re developing the Si7021 interface code as a reusable component, we need to structure our project in such a way that we can easily refer to it in our main code.

How to disable links opening in a new tab

web
One of the most infuriating UX choices on the web is the developer’s choice to open every single link in a new tab. There are a few sites I interact with, including Aliexpress, where the designers have inflicted this on the users. Fortunately, I found a solution. The Chrome extension target="_blank"-toggler works well. When you hover over a link which would open in a new tab, it superimposes a visual signal.

Setting up Arduino IDE with Heltec ESP32 module for macOS

Heltec WiFi Kit 32 ESP32 module The Heltec WIFI Kit 32 is an interesting little module that integrates a WiFi/MCU SoC and a small OLED display on a single board. If you want to set up the Arduino IDE to work with this device and you’re on macOS, this is for you. This particular ESP32 module has a number of impressive features: 240 MHz processor speed and 4 MB of flash memory.

New Yorker: Michael Cohen and the end stage of the Trump presidency

Brutal piece by Adam Davidson about the pivotal role that the takedown of Michael Cohen plays in the unraveling of this disastrous presidency. The narrative that will become widely understood is that Donald Trump did not sit atop a global empire. He was not an intuitive genius and tough guy who created billions of dollars of wealth through fearlessness. He had a small, sad operation, mostly run by his two oldest children and Michael Cohen, a lousy lawyer who barely keeps up the pretenses of lawyering and who now faces an avalanche of charges, from taxicab-backed bank fraud to money laundering and campaign-finance violations.

NYT: Tethered to a buffoon

A brutal piece on Trump from the New York Times. There are plenty of examples these days, from Moscow to Budapest, of how “democracies” can be manipulated to the point where they can yield only one result. This is Trump’s objective, and for it he needs a weakened Justice Department, a weakened press and an American public that will believe anything. He has had setbacks but is stubborn. Trump’s toolkit is familiar.