Measuring motor speed and display result on LCD using ATmega8 microcontroller

Summary of Measuring motor speed and display result on LCD using ATmega8 microcontroller


This article describes a motor speed measurement system using an H21A1 optical interrupter and an ATmega8 microcontroller. The device detects holes in a disk attached to the motor axis, generating pulses for the ICP pin to calculate RPM via timer interrupts. Results are displayed on a 2x16 LCD connected to specific AVR ports.

Parts used in Measuring Motor Speed with ATmega8:

  • H21A1 Optical Interrupter
  • ATmega8 Microcontroller
  • PWM Disk with holes
  • 2x16 LCD Display
  • AVR Microcontroller Ports (PORTD, PORTC)
  • Timer Pre-scaler (configured for 8MHz clock)

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

RPM=60000000/T

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

Quick Solutions to Questions related to Measuring Motor Speed with ATmega8:

  • How does the H21A1 device work?
    It uses a coupled IR LED and photo-transistor where an opaque material interrupts the signal to switch the output from ON to OFF.
  • What is the function of the PWM disk in this project?
    The disk is fixed to the motor axis; each time its hole passes the gap, it forms a pulse that triggers the timer.
  • Which pin on the Atmega8 is used for input capture?
    The ICP pin, which corresponds to PB0 on the Atmega8, is used to receive pulses from the optical interrupter.
  • How is the timer frequency calculated for the 8MHz clock?
    With a pre-scaler of 8, the timer runs at 1MHz, resulting in a period of 1μs.
  • What formula is used to calculate RPM?
    RPM equals 60000000 divided by T, where T is the duration of one disk turn.
  • How are the LCD data pins connected to the AVR?
    LCD data pins are connected to AVR PORTD.
  • Where are the LCD control pins connected?
    LCD control pins RS, R/W, and E are connected to PC0, PC1, and PC2 respectively on AVR PORTC.
  • When does the input capture interrupt occur?
    The interrupt occurs on the falling front of the pulse reaching the ICP pin.

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:
LinkedinTwitter
Scroll to Top