Thursday, February 25, 2021

Playing music and tones using a piezo buzzer - library for AVR microcontrollers using timer interrupt | ATmega328P, ATmega88

This is a library for playing chiptunes music using PWM and a piezo buzzer. Tones with different frequencies and timeouts can also be produced. The library uses a timer interrupt so the functions are non-blocking meaning that while a song or tone is generated the CPU can do other things. Timer 1 is used for PWM and another 8 bit selectable timer (0 or 2) is used to trigger the interrupt every 1ms. When a note ends the code inside the interrupt will calculate the PWM frequency of timer 1 based on the next note.

Optionally a led can be made to blink with the music rhythm.

The piezo buzzer must be connected to pins OCR1A and OCR1B which are set to have opposite polarity - when one pin is high the other one is low, then the polarity is reversed. This way you can make the buzzer louder with a lower voltage because the buzzer will see 10V even if the microcontroller outputs 5V.

Playing music and tones using a piezo buzzer - library for AVR microcontrollers using timer interrupt | ATmega328P, ATmega88

Types of buzzers

Before going any further I should clarify regarding the types of buzzers for those who are not familiar with them.

There are passive buzzers and active buzzers.

Saturday, February 6, 2021

Colorspace conversion between RGB and HSL library code for AVR microcontrollers | ATmega328P

I had a project where I needed to crossfade RGB colors and I thought why not using the HSL color space instead of the RGB color space because with the HSL the code looks neater and the Hue (color), Saturation and Lightness can easily be modified to create all kinds of light effects.

Colorspace conversion between RGB and HSL library code for AVR microcontrollers | ATmega328P

Converting HSL to RGB

HSLtoRGB(hue, saturation, lightness, rgb[])

Thursday, February 4, 2021

How to control RGB leds | RGB fader library for AVR ATmega328P

RGB leds are fun and because they can be used in many projects I have decided to make a library to easily crossfade the colors of one or multiple RGB leds. Because there is plenty of information about how an RGB led works I will not be talking about how the led works but how to crossfade between the 3 colors.

How to control RGB leds | Crossfading RGB leds | Library for AVR ATmega328P

Crossfading an RGB led in the RGB colorspace

Wednesday, February 3, 2021

Multi-channel software PWM library for AVR microcontrollers | ATmega328P

What you do when you run out of PWM pins on hardware? You make software PWM of course. This library is based on "AVR136: Low-Jitter Multi-Channel Software PWM" application note. It supports up to 10 PWM channels (more can be added) and it's suitable for led dimming, DC motor control and RGB led controller.

Multi-channel software PWM library for AVR microcontrollers | ATmega328P

Since this method is already explained in the AVR136 app note I won't go in to too many details. So the basic principles behind software PWM are this. A timer interrupt is set to trigger every 256 system clocks. For 8 bit timers the interrupt is on overflow and for 16 bit timers is on compare match. A 16 bit timer has the advantage that the base frequency can be modified. On every interrupt a variable "softcount" is incremented from 0 to 255 and each time is compared against each PWM channel. At the beginning of the cycle the pins are set high and when "softcount" equals to a channel's set value then the specific pin is set low. On 8MHz CPU the ISR takes between 4 and 10us to execute the code depending on how many channels and on how many ports there are. The size of the "softcount" variable dictates the PWM resolution and it is set to 8 bits. 

Saturday, January 23, 2021

Binary Code Modulation (BCM) aka Bit Angle Modulation (BAM) library for fading leds | ATmega328P

Binary Code Modulation (BCM) it's an amazing method for led dimming and was invented by Artistic Licence. It's like PWM but not really. The main advantage over PWM is the low CPU usage regardless of how many leds it controls.

This library provides a fast implementation of Binary Code Modulation useful for controlling RGB leds and dimming multiple leds for creating animations like led cubes and includes an array for logarithmic brightness. A complete cycle takes 8 timer interrupts and each interrupt takes only 4us on a 8MHz CPU. The leds can be on different ports.

Bit Code Modulation (BCM) aka Bit Angle Modulation (BAM) library for RGB led dimming - 8-bit

How Binary Code Modulation (BCM) works and how it differs from PWM

To dim a led with PMW is simple. If you want the led to be half as bright you turn the led on for 50% of the cycle and 50% for the other half. Or 20% on and 80% off for an even dimmer led.

 

PWM example

Bit Angle Modulation uses the weight of each bit in a binary number. For example in one byte there are 8 bits with numbers from 0 to 7. Bit 0 is called the Least Significant Bit (LSB) and it's weight is 1. Next bit 1 has a weight of 2, bit 2 has a weight of 4, then 8, 16, 32, 64 and 128. Bit 7 is called the Most Significant Bit (MSB) because it has the highest weight - 128.

8-bit binary weight

Monday, January 18, 2021

7 segment display library for AVR microcontrollers | ATmega328P

There are many ways to control a seven segment display - using a dedicated IC or shift registers which are preferred because they don't require many pins. However this library is made for when you have the segments driven directly from microcontroller pins and each digit is controlled using a transistor.

You have the option of padding the numbers with zeros and displaying them at a certain position, useful for making digital clocks.

Seven segment display library for AVR microcontrollers

What is a 7-segment display

As the name suggests it is a display that is made up of 7 segments. Each segment is simply an LED. Including the dot there are actually 8 LEDs and this fits perfectly on an 8-bit microcontroller's port. This display is mainly made for numerical values but some alphabetical characters can be displayed as well.

Types of 7 segment displays

There are two types of seven segment displays - common cathode and common anode. Common cathode displays have all the ground sides (cathodes) of the LEDs connected together while common anode displays have all the positive sides (anodes) of LEDs tied together.

The 7 segment display can have from 1 up to 6 or 8 digits. One digit can display numbers from 0 to 9 and a dot for numbers with decimals. On 4 digit seven segment display the maximum number that can be displayed is 9999.

7 segment display internal equivalent circuit OPD-Q5621LE-BW
Figure 1: Internal equivalent circuit of a 7 segment display from OPD-Q5621LE-BW datasheet

 

Saturday, January 16, 2021

Library for reading and debouncing multiple buttons using interrupt for AVR microcontrollers | ATmega328P

This library provides an easy way for reading and debouncing one or many buttons connected to a microcontroller. It can also read a button combination, button long press and button double press (like a double click). The response time is very fast without false triggers. Reading and debouncing takes only 50us every 10ms. It uses Binary Button Debouncing (BBD) method.

Button debouncing library for AVR microcontrollers

What is button debouncing anyway?