Temperature and humidity measurement with the AM2120

FunduinoLast updated on

Zwei Messwerte über eine einzige Datenleitung auslesen

Der AM2120 misst Temperatur und Luftfeuchtigkeit und gibt beide Werte über eine einzige Datenleitung an den Mikrocontroller weiter.

In dieser Anleitung schließt du den Sensor an, installierst die passende Bibliothek und lässt dir die Messwerte im seriellen Monitor anzeigen. Achte dabei besonders auf die Kabelfarben – bei diesem Sensor sind sie anders belegt, als man es erwarten würde.

Complete setup: AM2120 sensor on Arduino UNO, data line connected to pin 5

You need this

AM2120 temperature and humidity sensor - JST connector, 1-Wire AM2120 temperature and humidity sensor - JST connector, 1-Wire 5,98 € *Item no. F23107861About the product — AM2120 temperature and humidity sensor - JST connector, 1-Wire
Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino 11,12 € *Item no. F23107480About the product — Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino
Breadboard Cables Male-to-Male - 40 Pieces, 20 cm Breadboard Cables Male-to-Male - 40 Pieces, 20 cm 1,74 € *Item no. FUN-1010558About the product — Breadboard Cables Male-to-Male - 40 Pieces, 20 cm

All 3 parts together 18,84 € *

* All prices include statutory VAT, plus shipping costs.

Connect the sensor

The AM2120 measures temperature and humidity and outputs both values over a single data line. Connect the data line to pin 5 of the microcontroller.

Important: there’s a catch.

The AM2120 comes in various forms. In the version with the small housing, the wire colors don’t match the usual pinout. You would normally expect red for positive, black for negative, and yellow for the data line - but this version is different:

  • Yellow to 3.3V
  • Black to GND
  • Red is the data line and connects to pin 5

It’s best to check the pinout yourself: look at the sensor and trace the contacts to the wires. The pinout from the datasheet is shown in the first image.

The parts list specifies 40 male-to-male jumper wires. However, the linked sensor has a JST connector with leads, so you need male-to-female wires or a suitable JST cable. The article does not explain why the sensor is powered with 3.3 V instead of 5 V; according to the datasheet, both voltages are permitted.

AM2120 pinout according to the datasheet: yellow wire to 3.3V, black to GND, red as the data line
Complete setup: AM2120 sensor on Arduino UNO, data line connected to pin 5
1 / 2

Install the DHT library

You need a library for programming. The AM2120 uses the same communication method as the DHT22 and is therefore compatible with the DHT library.

If it isn’t already available in your Arduino IDE, install it using the Library Manager.

The library manager of the Arduino IDE with the DHT library

The sketch

In the setup section, you start serial communication and connect to the sensor. In the loop section, you read the temperature and humidity into separate variables.

The isnan() check verifies that the values read are valid. If the sensor does not return a usable value, the sketch displays an error message and starts the loop over. Only then are the readings displayed in the serial monitor.

Read temperature and humidity
#include "DHT.h"
#define pinDHT 5 // Sensor pin
DHT dht(pinDHT, DHT22); //Create a sensor from the library, configured for the DHT22 type, which uses the same communication method as the AM2120
void setup()
{
  Serial.begin(9600); //Initialize serial communication
  dht.begin(); //Start communication with the DHT sensor
}
void loop()
{
  float temperatur = dht.readTemperature(); //Read the temperature into a variable
  float luftfeuchtigkeit = dht.readHumidity(); //Read the humidity into a variable
  //Check whether the readings are valid using the isnan function
  if (isnan(temperatur) || isnan(luftfeuchtigkeit)) //Display an error message if reading fails
  {
    Serial.println("Error reading sensor");
    delay(800);
    return;
  }
  //Output the measured values via the serial interface
  Serial.print("Temperature: ");
  Serial.print(temperatur);
  Serial.print(" degrees Celsius, ");
  Serial.print("Humidity: ");
  Serial.print(luftfeuchtigkeit);
  Serial.println("% RH.");
  delay(800);
}
Rate the instructions

Comments

Ask a question or share an experience

Contact us if something doesn’t work. Please include your board and the error message.

We review every comment before publication. Your email address remains with us.