How to integrate Arduino Mega with Home Assistant

Here’s my setup:

  • HA (HassOS 4.15) running on an R-Pi

  • Linux Debian 10 laptop through which I use Firefox (v78.4.0esr) to configure / communicate with HA

  • I have also set up SSH to get to HA through a terminal window on the laptop

  • Arduino Mega 2560 has firmata v2.5.8 library installed

I am trying to set up HA to work with my Arduino Mega - this is a brand new setup and a learning experience for me. Thanks for the patience. Here goes:
Here’s a simple view of the hardware:

My configuration.yaml has the following:

firmata:

  • serial_port: /dev/serial/by-id/usb-Arduino__www.arduino.cc__0042_559303435363510141F2-if00
    serial_baud_rate: 57600
    sensors:
    • name: DHT22_1
      pin: A0
      pin_mode: ANALOG
      differential: 3

I find the following error Configuration / Logs

Logger: homeassistant.components.firmata.config_flow
Source: components/firmata/config_flow.py:41
Integration: Firmata (documentation, issues)
First occurred: 9:01:13 AM (1 occurrences)
Last logged: 9:01:13 AM

Error connecting to PyMata board serial-/dev/serial/by-id/usb-Arduino__www.arduino.cc__0042_559303435363510141F2-if00:

Hi Randy,

I can’t help with your specific question and while I don’t mean to discourage your experiment, you’re probably better off looking at an ESP8266 (eg, Wemos D1 Mini) and using ESPHome or Tasmota to get the DHT22 working. This way you can use a WiFi connection and learn things such as MQTT and if you want to use C++ well that’s available to you too.

Arduino is a great platform but has been largely usurped by more capable WiFi enabled boards as they do the basics and so much more.

Happy experimenting!

Yeah, I have ordered two 8266s that have not arrived yet. My goal is to get the integration working both ways. I’ll still want touse the Arduino for the number of ports.

You can’t use Firmata to read a DHT sensor.

See documentation:

“The arduino sensor platform allows you to get numerical values from an analog input pin of an Arduino board. Usually the value is between 0 and 1024.”

If you want to read a DHT you’ll need to write some custom code to either output values to the serial port, or modify the firmata to fool the values to an analog pin value, or connect up an Ethernet and send the values via HTTP, MQTT, etc.

This, this is not remotely true. :slight_smile:

Especially keeping in mind that most ESP8266 projects are using the Arduino framework.

Thank you for your input. I don’t know if you’ve seen it, but the link to [Arduino] that you provided states at the very top line:

This integration is deprecated. Please move to the Firmata integration.

So it’s not too useful if I’m trying to configure something that is of current era.

Firmata (as I think I’ve come to understand) only provides for

What Firmata apparently does not handle is PWM input. The DHT22 is PWM output.

I know that and do it myself. The comment was in reference to the older non-WiFi capable hardware :wink:

I’m not sure, at this point, how any of your comments were intended to lend me greater understanding. Apologies. I still don’t know what further understanding of Arduino<->Home Assistant you’re aiming to convey.

I had accidentally posted the wrong link, as it leads to the current Firmata integration where I took the information to reply from.

However, the statement stands. You cannot use the Firmata integration to read from a DHT sensor.

DHT sensors are not PWN output sensors.

DHT sensors use a custom digital protocol. You can read about it in the link below however to summarize: The data line is held high. To start a read, the MCU pulls the line low for 18ms, then high for 40 us. After which, the DHT now pulls the line low for 80us, then high for another 80, the begins transmitting its data by alternately pulling the line low and high for specific time periods to represent 1s and zeros. 50us low then 26-28us high for 0, or 70us for 1. The MCU reads this pulse stream, evaluates it and pulls out the bits to construct a reading.

Incidentally this precise timing is why this sensor is such a pain on MCUs like the ESP8266 which have background functions running. You can’t necessarily guarantee timing without disabling interrupts, etc.

Point is, it’s not PWM. :slight_smile: Firmata cannot read it.

You can read the sensor via the standard Arduino DHT libraries and send via serial, or Ethernet. Or you can modify the Firmata code to translate the readings of the sensor into false analog input values between 0 and 1023 which you can feed to Firmata in place of the direct analog pin reading.

Thanks for help. I don’t know where I read that DHT22 out is PWM. I’m grateful to be corrected.
Thanks!

Hi,
did you fix the problem with the Arduino Mega 2560?
I have exactly the same case, after changing to Arduino Uno there is no problem.

I needed to put a pause on my Arduino project. However here is a PWM temp sensor.

I wanted to use the arduino as a boolean logic sensor because of the large number of pins.

I use a modified version of OpenMQTTGateway for this.
I have lots of door sensors, motion sensors, temp/humidity sensor and a light sensor wired to it. Connected to the network through ethernet cable.
This is the mega I am using. Its been running for more than a year without any issue.

OG can directly talk to HA or through MQTT but the old version I am running did not have the HA integration, I am talking to HA trough MQTT.