Thursday, May 24, 2018

Analog to Digital Converter (ADC) library for AVR microcontrollers

This library provides a quick and easy way to set up an ADC on AVR microcontrollers and retrieve the values in 8-bit or 10-bit format in an interrupt driven fashion.

Nowadays even the cheapest microcontroller has a build-in ADC (Analog to Digital Converter). An ADC converts analog signals into digital signals and can be used in a wide range of applications like recording a signal from a microphone into a digital format, reading light sensors like an LDR (light dependent resistor), measuring current consumption, reading temperature or humidity sensors, etc. All these requires voltage measurements that an ADC can do.



Wednesday, May 23, 2018

Watchdog library | AVR microcontrollers | ATmega328P

Even if you are a dog or a cat person, you should still be using the watchdog.

For those who don't know, a watchdog is a timer inside the microcontroller generated by an RC oscillator - with 128kHz frequency on an AVR device. When it times out, the microcontroller is reset. To prevent it from resetting the MCU, the watchdog timer must be reset by the code inside the while loop. The idea is that if you have a loop and gets stuck, the watchdog timer will not be reset and so the microcontroller will be reset after the timer reaches the timeout period. Say you read a sensor and in a loop the code waits for the sensor response but the sensor is malfunctioning. If not for the watchdog, the MCU will get stuck and your drone will crash.



Watchdog extension library for AVR microcontrollers

On ATmega328 the available timeouts are 16ms, 32ms, 64ms, 0.125s, 0.25s, 0.5s, 1s, 2s, 4s, 8s. Choosing the right timeout depends on the specific application. The while loop must be able to finish executing the code and reset the watchdog timer before the timer runs out. For critical applications where if the CPU being stuck for more than 1 second is unacceptable, you can choose timeouts of a few milliseconds. Those cases can be a drone where reaction time needs to be fast, or a 3D printer reaches the end and the motor needs to be stopped in time. But in most cases the timeout can even be 8s, like when taking room temperature readings.

Other uses for the Watchdog timer

Tuesday, May 8, 2018

Library for reading multiple buttons with 1 ADC pin | AVR microcontrollers

There are many ways of reading switches, and one of them is using ADC pins. The advantages of this method is that it uses very low pin count compared to other methods, and all is needed are switches, resistors and an ADC (analog to digital converter). With a 10bit ADC, at least 20 switches can be used per pin.

To read the buttons the conventional way where every button is connected directly to a GPIO pin, check out this other library https://www.programming-electronics-diy.xyz/2021/01/button-debouncing-library-for-avr.html.

Library Features

  • Supports multiple groups of buttons on different ADC pins
  • Ability to read combination of multiple button press on different ADC pins
  • Can have different number of buttons on each pin
  • Check if a button was pressed for a certain amount of time
  • Timer 2 and ADC is setup by default 

Reading buttons using ADC (configuration #1)

Reading buttons using ADC - Configuration 1
Fig. 1 Reading buttons using ADC (configuration #1)