In this example we will connect a AM2301 sensor to an Arduino and display the output on an 128×32 I2C OLED display
The AM2301 is a wired version of the DHT21, in a large plastic body.
Specifications:
- Type: AM2301
- Accuracy resolution: 0.1
- Measurement range: 0-100%RH
- Temperature measurement range: -40℃ ~ +80℃
- Humidity measurement precision: ±3%RH
- Temperature measurement precision: ±0.5℃
This Sensor has 3 wires, connect these wires to your Arduino like this
Sensor | Arduino |
---|---|
Red | +5V |
Black | GND |
Yellow | Digital I/O |
OLED Connection
Connect Vin to 5V.
Connect GND to ground
Connect SDA to A4
Connect SCL to A5
Code
The code uses the adafruit library for the DHT11 and other variants
Library – https://github.com/adafruit/DHT-sensor-library
OLED library was from Adafruit
Libary – https://github.com/adafruit/Adafruit_SSD1306
[codesyntax lang=”cpp”]
#include <SPI.h> #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <stdint.h> #include "DHT.h" #define DHTPIN 2 // modify to the pin we connected #define DHTTYPE DHT21 // AM2301 #define OLED_RESET 4 Adafruit_SSD1306 display(OLED_RESET); DHT dht(DHTPIN, DHTTYPE); #if (SSD1306_LCDHEIGHT != 32) #error("Height incorrect, please fix Adafruit_SSD1306.h!"); #endif void setup() { Serial.begin(57600); dht.begin(); display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32) } void loop() { // Clear the buffer. display.clearDisplay(); // text display tests display.setTextSize(1); display.setTextColor(WHITE); display.setCursor(0,0); float hum = dht.readHumidity(); float temp = dht.readTemperature(); // check if returns are valid, if they are NaN (not a number) then something went wrong! if (isnan(temp) || isnan(hum)) { Serial.println("Failed to read from DHT"); } else { display.print("Temperature: "); display.print(temp); display.print(" c"); display.setCursor(0,10); display.print("Humidity: "); display.print(hum); display.display(); delay(2000); } }
[/codesyntax]
Output
All going well you should see output like the following on your OLED display
Links
You can pick up one of these breakout/modules for under $8