-Example AVR Projects

AVR-GCC LCD library – mixed pin support

Some time ago we have posted alphanumeric AVR-GCC LCD library. It works fine in 8-bit and 4-bit modes. But it has some limitations that some people may find annoying. One of them is requirement that LCD pins has to be byte aligned for instance in 8 bit mode LCD_D0 … LCD_D7 pins has to be connected to AVR single AVR port. Similar situation is with 4-bit mode where LCD data pins has to be connected to single port 4, 5, 6 and 7 pins. For both modes control pins RS, RW and E has to be connected to single port.


And this is how most LCD libraries work when you try to look for one in the internet. In reality things may be a bit different each microcontroller pin has at least several alternative functions available like ADC, INT, I2C, USART and if project requires using one or another and you still need LCD standard libraries won't work as most likely you wont be able to get all particular port pins connected to LCD. You gotta use whats left. This is why I decided to find a little time and modify LCD library to support these cases. Didn't want to write everything from scratch or change its functionality – just wanted it work with existing projects but have more freedom with new ones. So basically I left standard 8-bit and 4-bit same. The main change is adding two more modes: 8-bit mix and 4-bit mix. These modes allow connected LCD to any free pins of microcontroller.

Temperature sensor with time and date display on graphical LCD

Some time ago I've build a prototyping board with graphical LCD. It have served for various small projects and prototypes. Had a spare temperature sensor DS18B20 and decided to put simple temperature display project. GLCD board is equipped with Atmega32 microcontroller running at 16MHz. DS18B20 sensor is connected to port D pin 6.

Temperature sensor with time and date display on graphical LCD

LED connected to PD3 is used for indicating EEPROM write activity. Device is navigated with rotary encoder. It is connected to MCU as follows (more about interfacing rotary encoder here):

Interfacing rotary encoder to Atmega32

Recently I was working on a project that involved rotary encoder. I thought I'd share some thoughts on how rotary encoder can be interfaced and programmed. Actually it is easy to work with rotary encoders - interfacing is simple – only three wires are required to connect to microcontroller (two for signal (quadrature outputs) and one for reference (GND)). When turned there is a Grey code produced on outputs that allows tracking turn speed and direction. These features allow having convenient user interface with single knob. Many rotary encoders also comes with button – so menu navigation couldn't be easier. In our project we are going to use a 12-step mechanical rotary encoder from sparkfun.

Rotary encoder

We are going to interface it to ATMega32 board with graphical LCD.

Running TX433 and RX433 RF modules with AVR microcontrollers

Sometimes in embedded design you may want to go wireless. Might be you will want to log various readings of remotely placed sensors, or simply build a remote control for robot or car alarm system.

Radio communications between two AVR microcontrollers can be easy when specialized modules are used. Lets try to run very well known RF modules TX433 and RX433 that (or similar) can be found almost in every electronics shop and pair of them cost about ~15 bucks.

Transmitter and receiver modules are tuned to work correctly at 433.92MHz. Transmitter can be powered from 3 to 12V power supply while receiver accepts 5V. 5V is common for AVR microcontrollers so no problems with interfacing. Modules don't require addition components – just apply power and connect single data line to send information to/from and that's it. For better distances apply 30 – 35cm antennas. Modules use Amplitude-Shift Keying(ASK) modulation method and uses 1MHz bandwidth.

Programming AVR ADC module with WinAVR

Most of AVR microcontrollers have Analog to Digital Converter (ADC) integrated in to chip. Such solution makes embedded designers life much easier when creating projects and programming them. With no need external ADC PCB takes less space, easier to create programs – it saves time and money. As an example lets take Atmega8 microcontroller which have up to 8 ADC inputs most with 10-bit resolution(excluding ADC4 and ADC5 inputs that are 8-bit). All features of AVR internal ADC can be found on official ATMEL AVR datasheets, but most important to mention are:

  • ±2 LSB accuracy – so measurements aren't very accurate. If AREF voltage is 5V then error may reach ±0.04V but this is still good results for most of tasks;

  • Integral nonlinearity ±0.5 LSB;

  • Conversion speed up to 15kSPS at maximum resolution. This is far not enough for 20kHz audio signal sampling.

ADC unit is powered with separate power supply pins AVCC with AGND, but AVCC must not differ ±0.3V of VCC. Also ADC unit can have different voltage reference sources selectable in ADMUX register. References may be taken from AREF pin, AVCC with external capacitor or internal 2.56V voltage reference. All ADC inputs are multiplexed via multiplexer. Each channels can be selected by changing 4 bits in ADMUX register. ADC unit can operata in two modes:

Simple signal drawing on graphical LCD routines

During spare time I have been playing with graphical LCD. This time I decided to display simple signals that are stored in microcontroller memory. The idea was to read signal values from look-up table and display waveform on Graphical LCD. To make things more interesting I divided LCD screen in to smaller four screens so I could activate them separately and draw signals in them.

Graphical LCD is the same old HQM1286404 with KS0108 controller. I have used Proteus simulator 128×64 graphical LCD(LGM12641BS1R) which is based on KS0108. How to implement and connect LCD there was a blog post (Simulate KS0108 graphical LCD with Proteus simulator

)about it. I am just going to show main program routine.

As I mentioned I have split 128×64 in to four smaller screens like this:

Output number when button is pressed

This is simple demo program of reading button state, lighting LEDs, sending information via USART. 8 buttons are connected to Atmega16 port A, 8 LEDs to port B via current limiting resistors. While none of buttons arent pressed there is running light on LEDs performed, but when any of buttons is pressed then LEDs display current 8 buit counter value in binary format. Same value is sent via USART – you can see number in terminal if connected.

AVR-GCC 4 bit and 8 bit LCD library

Standard alphanumeric LCD display controlled by 74HC164 LCD controller can accept 8 bit data bytes or 4 bit nibbles. Earlier my 4 bit and 8 bit LCD libraries were split in separate files as they were used in different projects. Now they are merged in to one library where simple logic is implemented to select 4 bit or 8 bit library just by modifying only three lines of code.

In the library header file there is line added:


//Uncomment this if LCD 4 bit interface isused


#define LCD_4bit



what allows to select different LCD modes by commenting and uncommenting this line. Also don't forget to select proper ports and pins where LCD is connected:

AVR 4-bit LCD interface library

Standard alphanumeric LCD display controlled by 74HC164 controlled can accept 8 bit data bytes or 4 bit nibbles. Using 4 bit interface may give few benefits like you can save 4 microcontroller pins and use them for different purposes, or use small pin count Microcontrollers to control LCD like AVR ATtiny series.

When using 4 bit mode, only four data-lines of LCD (D4...D7) are used. So first is high nibble sent and then lower nibble has to be sent in order to form single byte. This way there are two cycles used to form one data or command byte. Earlier I have been using LCD library from Procyon AVRLIB, but I wasn’t satisfied with it as it generated a lots of HEX code because of many functionality included.

Measuring motor speed and display result on LCD

For measuring motos speed there can Optical interrupter used like H21A1. This is a device where IR LED and photo-transistor is coupled in to plastic housing. The gap between then allows interrupting signal with opaque material and this way switching the output from ON to OFF.

This device can be connected to Microcontrollers ICP pin and this way measuring PWM disk (with hole in it) speed can be measured. Disk has to me fixed to axis of motor. Each time the hole of disk passes the gap, optical interrupter will form a pulse which goes to ICP pin to trigger the timer. If take measuring interval 1s, then counted pulses will be equal to turns in Hz.

Lets take Atmega8 microcontroller which is clocked at 8MHz. For this lets use timer pre-scaler 8, then timer will run at frequency equal 1MHz(period 1μs ). Each time the pulse reaches ICP(Atmega8 – PB0 pin) pin then on falling front of pulse input capture interrupt occur. Interrupt service routine counts the number of timer pulses between two pulses. Number of timer counts define the disk speed (RPM - revolutions per minute).


Subscribe to RSS - -Example AVR Projects