Howto use an arduino with AM2302 sensor to read temperature and humidity in Hassio?

Hi all,

I am trying to get a temperature sensor into Hass.io by using an Arduino UNO with an AM2302 temperature sensor. I have installed StandardFirmata and I can see the sensor in Hass.io
I am not able to see any temperature, and I don’t know how to set the wright pin.
When I try to set the pin like “A0” analog input Hassio wont accept that and throws an invalid option at me.
image

This is what the docs say, The 6 analog pins of an Arduino UNO are numbered from A0 to A5.

My config looks now like this, but there is no data coming into Hass.io:

# configuration.yaml
arduino:
  port: "/dev/ttyUSB1"

# sensor.yaml
- platform: arduino
  pins:
    0:
      name: "Temp arduino"

Is there someone who can help me with this?
Thanks in advance!

I think A0 is actually pin 14 on the UNO, you would need to check the pinout.

@PrairieSnpr Thank you for your reply.
A0 is pin A0 and GPIO14, I have tried to put 14 in my config (where now is 0) but this does not work also.
It will give me this error in Hass.io

image

When I use the 0 it gives me this
image

fluctuating from 1020 to 1023
image

I don’t know how to make something out of this…

Ok, assuming that’s a legit reading, I don’t think it is because the value should range from 0 to 1024, so 1023 would be a value equal to AREF, and so likely not your sensor. But when you do get the correct pin setup, you will need to calculate the temperature from your reading.

– Wait –
Just realized you are using a form of the DHT22, that’s not going to work. Those use a form of one wire communication, not an analog output. You would need a thermistor if you want to use an analog input on the arduino.

See here for a guide on reading temp using a DHT22.

Ill tell you what would be a whole lot easier: Get a ESP8266 or ESP32 board and use esphome.io.

Thank you for thinking with me, but that is not what I am trying to achieve.
I have some unused arduino boards that I am trying to put to use.
I also have some Wemos and nodeMCU so I will try esphome for sure,
but for now I just want the arduino to work!

I don’t think the home assistant arduino support is going to work for DHT22. Firmata doesn’t seem to support it (in my limited googling), and if firmata doesn’t support it, it ain’t gonna work.

What about the serial sensor?

Thank you for this option @PrairieSnpr!
I run into some problems with compiling the sketch.
First problem: library ArduinoJSON,
*

StaticJsonBuffer is a class from ArduinoJson 5. Please see Redirecting… to learn how to upgrade your program to ArduinoJson version 6

to solve this I downgraded to version 5.13.0 (from 6.10.0)

Next problem: Fout bij het uploaden: configuratie-parameter ‘serial.port’ ontbreekt (Dutch)
In English: Error while uploading: configuration-parameter ‘serial.port’ is missing.

My serial port is: ‘/dev/ttyUSB1’ where should I put this?
And is it possible to read temperature and humidity from 1 pin? Both from analogRead(A0)?

```
#include <ArduinoJson.h>

void setup() {
  Serial.begin(115200);
}

void loop() {
  StaticJsonBuffer<100> jsonBuffer;
  JsonObject& json = prepareResponse(jsonBuffer);
  json.printTo(Serial);
  Serial.println();
  delay(2000);
}

JsonObject& prepareResponse(JsonBuffer& jsonBuffer) {
  JsonObject& root = jsonBuffer.createObject();
  root["temperature"] = analogRead(A0);
  root["humidity"] = analogRead(A1);
  return root;
}
```

Thanks for all the help!

I would start by getting the dht22 working on your Arduino separately first. Once you can read the temp and humidity values, then you can look at bringing them to HA.

Something like this should get you started.

#include <DHT.h>
#include <DHT_U.h>

//DHT
#define DHTPIN 6
#define DHTTYPE DHT22
DHT_Unified dht(DHTPIN, DHTTYPE);

void setup() {
  dht.begin();
}

void loop() {
  sensors_event_t event;
  dht.temperature().getEvent(&event);
  if (isnan(event.temperature)) {
    Serial.println(F("Error reading temperature!"));
  }
  else {
    Serial.print(F("Temperature: "));
    Serial.print(event.temperature);
    Serial.println(F("°C"));
  }
  dht.humidity().getEvent(&event);
  if (isnan(event.relative_humidity)) {
    Serial.println(F("Error reading humidity!"));
  }
  else {
    Serial.print(F("Humidity: "));
    Serial.print(event.relative_humidity);
    Serial.println(F("%"));
  }
}

It is also not clear to me that you have the sensor working with arduino yet? This is your first step… no sense in sorting out how to send data if you don’t even have the data to send.

I’d start off with the basic dht example code built in to the ide (after you install the required libraries for dhtxx sensors… adafruitunified.h, etc). Once you have meaningful data showing up on your serial monitor, then you should develop how that gets sent to hass via uart (arduinojson.h library makes this super easy).

That said, I only use serial sensor for some hardwired signals (doorbell, mains power, ups power, & backup battery volts). Those have wires running to my closet where my hass device is located. Since my temp sensors are spread across my home, those are mostly zwave, zigbee, or esp wireless devices. Keep this idea in mind… you may find it very useful to learn how to implement esp temp sensors, even if you don’t need to do it in this case. Esp devices are infinitely more useful than ‘uart only’ devices.

I’m don’t know if you ever found the solution, but this worked for me:

I used DTH11 temperature and humidity sensor with Arduino Uno.

I followed these instructions.

I uploaded this sketch to Arduino Uno (it is a slightly modified version of the sketch mentioned in the link above):

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

// REQUIRES the following Arduino libraries:
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
// - Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor

#include "DHT.h"

#define DHTPIN 2     // Digital pin connected to the DHT sensor
// Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 --
// Pin 15 can work but DHT must be disconnected during program upload.

// Uncomment whatever type you're using!
#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors.  This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  Serial.println(F("DHTxx test!"));

  dht.begin();
}

void loop() {
  
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);

  Serial.print(F("Humidity: "));
  Serial.print(h);
  Serial.print(F("%  "));
  Serial.print(F("Temperature: "));
  Serial.print(t);
  Serial.print(F("°C "));
  Serial.print(f);
  Serial.print(F("°F  "));
  Serial.print(F("Heat index: "));
  Serial.print(hic);
  Serial.print(F("°C "));
  Serial.print(hif);
  Serial.println(F("°F"));

  // Wait a few seconds between measurements.
  delay(2000);
}

(Obviously you can modify the code, e.g. remove the temperature in F, increase wait times between measurements, etc.)

In my Home Assistant configuration.yaml file I listed this:

sensor:
  - platform: serial
    serial_port: /dev/serial/by-id/usb-Arduino__www.arduino.cc__0043_75833353035351615292-if00
    baudrate: 9600

(You can find serial port your Arduino is connect to your HA under Supervisor > System > Host System > (three dots) > Hardware)