Yeshvanth Muniraj
Published

Analog to Digital Converter module of ATmega328P

Tutorial on how to use the ADC Module of the ATmega328P

IntermediateProtip1 hour23,839
Analog to Digital Converter module of ATmega328P

Things used in this project

Story

Read more

Code

Embedded C ADC

C/C++
#include<avr/io.h>                   // AVR/IO.H header file
#define F_CPU 16000000UL             // Setting CPU Frequency 16MHz
#include <util/delay.h>              // Delay Library 

#include <LiquidCrystal_I2C.h>       // I2C Module for LCD

LiquidCrystal_I2C lcd(0x3F,16,2);    // Address of the I2C module and LCD size

int ADC_O_1;                         // ADC Output First 8 bits 
int ADC_O_2;                         // ADC Output Next 2 bits   
void setup() {
  lcd.begin();
  lcd.clear();         
  lcd.backlight();
  lcd.setCursor(4,0);                //Set cursor to character 4 on line 0
  lcd.print("ADC Demo");
  _delay_ms(1000); 
  ADMUX = 0x40;
  ADCSRA = 0xC7;
  ADCSRB = 0x00;
}

void loop() {
  if ( (ADCSRA & 0x40) == 0)
  { 
    ADC_O_1 = ADCL;
    ADC_O_2 = ADCH;
    lcd.setCursor(0,0);
    lcd.print("ADCL:");
    lcd.print(ADC_O_1, BIN);         // Converting to binary form
    lcd.setCursor(0,1);
    lcd.print("ADCH:");
    lcd.print(ADC_O_2, BIN);
    ADCSRA |= 0x40;
    _delay_ms(600);
  }
}

Arduino Uno

C/C++
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F,16,2);   // set the LCD address to 0x3F 

const int sensor_pin = A0;
int value;
float volt = 0;

void setup() 
{
  lcd.begin();
  lcd.clear();         
  lcd.backlight();      
  pinMode (sensor_pin, INPUT);
  lcd.setCursor(1,0);              //Set cursor to character 2 on line 0
  lcd.print("Color Detector");
  delay(1000);
}

void loop() {
  value = analogRead(sensor_pin);
  val = value;
  volt = ((float(value))/(1023.0))*5.0;  // Converting to voltage
  if( value > 75 && value < 85)
  {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Voltage: ");
    lcd.print(volt);
    lcd.print("V");
    lcd.setCursor(0,1);
    lcd.print("Color: Red");
  }
  else if( value > 144 && value < 155 )
  {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Voltage: ");
    lcd.print(volt);
    lcd.print("V");
    lcd.setCursor(0,1);
    lcd.print("Color: Green");
  }
  else if( value > 188 && value < 200 )
  {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Voltage: ");
    lcd.print(volt);
    lcd.print("V");
    lcd.setCursor(0,1);
    lcd.print("Color: Blue");
  }
  else if( value >= 67 && value <= 71 )
  {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Voltage: ");
    lcd.print(volt);
    lcd.print("V");
    lcd.setCursor(0,1);
    lcd.print("Color: Purple");
  }
  else if( value > 118 && value < 130 )
  {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Voltage: ");
    lcd.print(volt);
    lcd.print("V");
    lcd.setCursor(0,1);
    lcd.print("Color: Cyan");
  }
  else if( value >= 65 && value< 67)
  {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Voltage: ");
    lcd.print(volt);
    lcd.print("V");
    lcd.setCursor(0,1);
    lcd.print("Color: Yellow");
  }
  else if( value >= 60 && value < 65 )
  {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Voltage: ");
    lcd.print(volt);
    lcd.print("V");
    lcd.setCursor(0,1);
    lcd.print("Color: White");
  }
  else
  {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Voltage: ");
    lcd.print(volt);
    lcd.print("V");
    lcd.setCursor(0,1);
    lcd.print("Color: Unknown");
  }
  delay(200);
}

Arduino Nano

C/C++
const int red = 2, green = 3, blue = 4;

void setup() 
{
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(blue, OUTPUT);
}

void loop() 
{
  digitalWrite(red, HIGH);
  digitalWrite(green, LOW);
  digitalWrite(blue, LOW);
  delay(5000);
  digitalWrite(red, LOW);
  digitalWrite(green, HIGH);
  digitalWrite(blue, LOW);
  delay(5000);
  digitalWrite(red, LOW);
  digitalWrite(green, LOW);
  digitalWrite(blue, HIGH);
  delay(5000);
  digitalWrite(red, HIGH);
  digitalWrite(green, HIGH);
  digitalWrite(blue, LOW);
  delay(5000);
  digitalWrite(red, LOW);
  digitalWrite(green, HIGH);
  digitalWrite(blue, HIGH);
  delay(5000);
  digitalWrite(red, HIGH);
  digitalWrite(green, LOW);
  digitalWrite(blue, HIGH);
  delay(5000);
  digitalWrite(red, HIGH);
  digitalWrite(green, HIGH);
  digitalWrite(blue, HIGH);
  delay(5000);
}

Credits

Yeshvanth Muniraj

Yeshvanth Muniraj

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

Comments