Skip to content

Latest commit

 

History

History
51 lines (33 loc) · 6.93 KB

README.md

File metadata and controls

51 lines (33 loc) · 6.93 KB

AVR-ASM-DHT22 🌡️

GPL 2 license badge

A picture of the device.

This image is Copyright (C) 2021 Nicholas Warrener - All rights reserved.

This project uses four seven-segment displays to show humidity and temperature measurements from a DHT22. A push button is used to switch between readings. A red LED turns on when the temperature is below 0°C. The device only shows measurements in Celsius! This project uses no libraries and is programmed entirely in AVR assembly.

Implementation Notes

In general, the device is interrupt driven so no delay loops are used! This makes for a much more elegant and efficient solution.

I find my implementation of the DHT communication protocol unique in the sense that it is almost completely interrupt driven. The algorithm only uses a timer to determine the duration of the pulses sent to it by the DHT22. This enables the 328P to determine if a zero or a one was sent by the DHT22. Since the communication code is interrupt driven (no polling or delay loops), you don't have to fiddle with timing as the code will wait until the DHT22 is ready to talk to it. By doing this, support for the DHT11 is enabled as you only need to change the initial delay value of the algorithm from 1ms to 18ms, and you also need to change the loop counter from 40 to 41 for the algorithm to work. You will need to make other changes to the code to get it to display the readings from the DHT11 properly.

The device still needs a software implementation of a debouncer for the push button. Currently no debouncer is used so you may need to press the push button a few times for the device to change the quantity being displayed.

The project uses an ATmega328P with its internal oscillator which is set to run at 8 MHz. By doing this, I have access to two more IO pins as no external crystal oscillator is connected to them. If you want to use this code on a 328P running at a different frequency, you'll need to recalculate the values for the timer output compare registers.

This device uses the watchdog timer (WDT) to make sure that the program does not get stuck waiting for a faulty or incorrectly connected DHT22. It is set to timeout every two seconds. If you make changes to the code and they cause the program execution to take longer than two seconds, you will need to increase the duration of the WDT. I recommend that you disable the WDT when making modifications and only once you know the modifications function correctly, then you should turn on the watchdog and see if it needs adjustment.

Making the Device

I've made an electrical schematic to show you how to assemble everything. You can find the schematic here.

I programmed the 328P using a USBasp ISP programmer. I don't have a bootloader on the 328P as the internal oscillator is inaccurate and will probably cause issues with serial communication. I'm assuming you're using a 328P that has not been used before. It should be set to use the internal oscilator. If it has been modified, you will need to restore the fuses to their factory configuration. You can do this and flash code using a USBasp and AVRDUDESS.

IF YOU DO NOT HAVE A SNAP THEN DO NOT SET THE DWEN FUSE, YOU WILL BE UNABLE TO COMMUNICATE WITH THE 328P. USE OF AN ON-CHIP DEBUGGER IS OPTIONAL.

I caved in and bought myself an MPLAB Snap which enabled me to perform on-chip debugging. This made debugging much less painful. To connect the Snap to the device, connect pin 2 of the Snap to VCC, pin 3 to GND and pin 6 to the reset pin on the 328P (PC6). If you want to use the Snap in Atmel Studio (now Microchip Studio), you need to enable the DWEN fuse on the 328P. Enabling this fuse will disable ISP programming and enable debugWire. To re-enable ISP programming, you need to open Atmel Studio, run a debug session, and then click the Debug menu at the top and click "Disable debugWire and close." Please see here for more information.

How to Use

Please do not use this device in applications where should the device malfunction and/or fail, injury or loss of life (of any kind) and/or property may occur!

When the device powers up, you will see "----" displayed on the segments. This informs the user that the device is on and that it is gathering its first measurement. If you see "----" after the device has been powered up for more than 15 seconds, then there is a fault with the DHT22.

If you connect PC5 to ground, the display will turn off. This indicates that the device is in flash mode. Flash mode sets the pins used by the ISP programmer to inputs with high impedance. Doing this enables the ISP device to communicate with the 328P.

Contributions

Contributions are welcome! Please file issues, or make pull requests, if you find something that could be improved or if there is an error.

Useful Things

  • I found this image here. It's nice to use when assembling the device and just as a reference to quickly find a pin on the 328P.

    The 328P pinout.

  • This website is a really nice tool to quickly calculate the value that needs to go in the output compare register (of a timer) to get a specific time.
  • This PDF from Dean Camera is a really nice read if you'd like to understand how to setup timers. He does it in C but it is very easy to port to AVR ASM.
  • You can find the instruction set for the 328P here. This is a nice reference when programming.
  • The datasheet for the 328P can be found here. The index is at the bottom of the PDF for some reason...