Yeshvanth Muniraj
Published

Analog Comparator Module in ATmega328P - Migrating to C

Tutorial on using Analog Comparator Module of ATmega328P.

IntermediateProtip1 hour8,942
Analog Comparator Module in ATmega328P - Migrating to C

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Rotary potentiometer (generic)
Rotary potentiometer (generic)
×2
LED (generic)
LED (generic)
×2
Through Hole Resistor, 470 ohm
Through Hole Resistor, 470 ohm
×2
Jumper wires (generic)
Jumper wires (generic)
×1
Breadboard (generic)
Breadboard (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Schematic

Breadboard connections

Code

Analog Comparator

C/C++
Tutorial on Analog Comparator
#include<avr/io.h>           // AVR/IO.H header file
#define F_CPU 16000000UL     // Setting CPU Frequency 16MHz
void setup() {
  DDRB = 0x03;               // Bit 0 and 1 as Output
  PORTB = 0x00;              // Clear all bits
  ACSR = 0x00;               // Clear bits of ACSR 
  ADCSRB = 0x00;             // Clear bits of ADCSRB 
}

void loop() {
  if( (ACSR & 0x20) == 0)    // Check ACO (output bit)
  {
    PORTB = 0x01;            // Turn on LED1 and turn off LED2 
  }
  else 
  {
    PORTB = 0x02;            // Turn on LED2 and turn off LED1
  }
}

Credits

Yeshvanth Muniraj

Yeshvanth Muniraj

19 projects • 35 followers
Hands-on experience in Embedded Systems and IoT. Good knowledge of FPGAs and Microcontrollers.

Comments