Measuring motor speed and display result on LCD using ATmega8 microcontroller

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.
Measuring motor speed

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).


T – duration of one disk turn. The results will be displayed on 2×16 LCD. LCD is connected to AVR as follows:

LCD data pins to AVR PORTD;

LCD control pins to AVR PORTC (RS->PC0, R/W->PC1, E->PC2).

#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include "lcd_lib.h"
#define RPM 60000000u
#define ICP PINB0
//timer overflow counter
For more detail: Measuring motor speed and display result on LCD using ATmega8 microcontroller

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top