Temperature Reading with ADC in MicroPython for ESP32 and ESP8266

In this project, you’ll learn Temperature Reading with ADC in MicroPython for ESP32 and ESP8266. We’ll use a temperature sensor, such as the TMP36, to measure the surrounding temperature and display the result. This project introduces concepts like ADC initialization and reading analog values, which are essential for working with analog sensors in embedded systems.

Purpose of the Project:

The purpose of this project is to help you:

  • Initialize the ADC in MicroPython for ESP32 and ESP8266.
  • Read analog values from a temperature sensor.
  • Convert the raw ADC values into readable temperature data.

Data Types and Variable Table for Temperature Reading with ADC in MicroPython

Data Type Variable Name Description
ADC adc Represents the ADC object reading the sensor data.
int value Stores the raw ADC value from the sensor.
float temperature Stores the calculated temperature in degrees Celsius.

Syntax Table for Temperature Reading with ADC in MicroPython

Operation Syntax Example
Initialize ADC adc = ADC(Pin(pin_number)) adc = ADC(Pin(34))
Read ADC Value adc.read() value = adc.read()
Convert to Temperature temperature = (value * factor) – offset temperature = (adc.read() * 3.3 / 4095 – 0.5) * 100

Required Components for Temperature Reading with ADC

  • ESP32 or ESP8266 board
  • TMP36 temperature sensor (or similar)
  • Jumper Wires
  • Breadboard

Circuit Diagram for Temperature Reading with ADC

        TMP36

         —–

         |—|—--> VCC (3.3V)  

         |—|—--> GND

         |—|—--> GPIO34 (ADC Pin)

 

Circuit Connection Table

Component Pin ESP32/ESP8266 Pin Explanation
TMP36 VCC Connected to 3.3V 3.3V Powers the temperature sensor.
TMP36 GND Connected to GND GND Ground connection for the sensor.
TMP36 Output Connected to GPIO34 GPIO34 (ADC Pin) Reads the analog voltage from the sensor.

Warning:

Ensure that you use the correct voltage for your sensor. The TMP36 operates at 3.3V, which is compatible with ESP32 and ESP8266.

Circuit Analysis for Temperature Reading with ADC:

The TMP36 sensor outputs an analog voltage proportional to the ambient temperature. The ADC on the ESP32/ESP8266 converts this voltage into a digital value, which we can use to calculate the temperature. The TMP36 outputs 0.5V at 0°C, with an increase of 10mV per degree Celsius.

Installing MicroPython and Libraries (If Needed):

No additional libraries are needed for this project. MicroPython’s built-in machine module handles ADC readings.

Writing the MicroPython Code for Temperature Reading with ADC:

import machine

import time

 

# Initialize ADC on GPIO34

adc = machine.ADC(machine.Pin(34))

 

# Function to convert ADC value to temperature

def read_temperature():

    value = adc.read()  # Read raw ADC value (0-4095 for 12-bit resolution)

    voltage = value * 3.3 / 4095  # Convert ADC value to voltage

    temperature = (voltage – 0.5) * 100  # Convert voltage to temperature (Celsius)

    return temperature

 

# Continuously read and display temperature

while True:

    temp = read_temperature()

    print(“Temperature: {:.2f} °C”.format(temp))

    time.sleep(1)

 

Running the Code and Checking the Output:

  1. Upload the code to your ESP32 or ESP8266 using Thonny or another MicroPython IDE.
  2. The code will read the temperature from the sensor and display it in degrees Celsius every second.
  3. As the temperature changes, you’ll see updated values in the console.

Explanation of the Code:

  • ADC Initialization: The TMP36 sensor is connected to GPIO34, and the ADC is initialized to read from this pin.
  • Temperature Conversion: The raw ADC value is converted to voltage, and then to a temperature in Celsius using the TMP36 sensor’s calibration (0.5V = 0°C, 10mV per degree Celsius).
  • Loop Function: The temperature is continuously read and printed every second.

Expanding the Project:

  1. Display on LCD: Display the temperature on an LCD or OLED screen instead of printing it in the console.
  2. Data Logging: Log temperature data over time and store it in a file for later analysis.
  3. Threshold Alerts: Add an LED or buzzer that triggers when the temperature goes above or below a certain threshold.

Common Problems and Solutions for Temperature Reading with ADC:

Problem Solution
Incorrect temperature readings Ensure the sensor is connected to the correct ADC pin and the voltage conversion is accurate.
ADC value not changing Check the wiring of the TMP36 and ensure it’s receiving power.
Temperature fluctuating too much Add a filter or take an average of multiple readings to smooth out the noise.

FAQ for Temperature Reading with ADC:

Q: Can I use a different temperature sensor?
A: Yes, but you’ll need to adjust the voltage-to-temperature conversion formula according to the sensor’s specifications.

Q: How accurate is the TMP36 sensor?
A: The TMP36 has a typical accuracy of ±2°C, making it suitable for general-purpose temperature monitoring.

Q: Can I use a different ADC pin?
A: Yes, as long as the pin supports analog input (ADC). Modify the Pin() initialization to use the correct GPIO pin.

Conclusion:

In this project, you learned how to read temperature data using ADC in MicroPython for ESP32 and ESP8266. You can now use this method to interface with other analog sensors that provide voltage-based output. This project is a great foundation for creating temperature-based monitoring systems, data loggers, or environmental control systems.