116 5. ANALOG TO DIGITAL CONVERSION (ADC)
//Set ADC module prescalar to 8
//critical for accurate ADC results
while (!(ADCSRA & 0x10)); //Check if conversion is ready
ADCSRA |= 0x10; //Clear Conv rdy flag - set the bit
binary_weighted_voltage_low = ADCL; //Read 8 low bits first (important)
//Read 2 high bits, multiply by 256
binary_weighted_voltage_high = ((unsigned int)(ADCH << 8));
binary_weighted_voltage = binary_weighted_voltage_low |
binary_weighted_voltage_high;
return binary_weighted_voltage; //ADCH:ADCL
}
//*************************************************************************
5.8 EXAMPLE: ADC RAIN GAGE INDICATOR
In this example, we construct a rain gage type level display using small light emitting diodes. The
rain gage indicator consists of a panel of eight light emitting diodes. The gage may be constructed
from individual diodes or from an LED bar containing eight elements. Whichever style is chosen,
the interface requirements between the processor and the LEDs are the same.
The requirement for this project is to use the analog-to-digital converter to illuminate up to
eight LEDs based on the input voltage. A 10k trimmer potentiometer is connected to the ADC
channel to vary the input voltage. We first provide a solution using the Arduino Development Envi-
ronment with the Arduino UNO R3 processing board. Then a solution employing the ATmega328
programmed in C is provided.
5.8.1 ADC RAIN GAGE INDICATOR USING THE ARDUINO
DEVELOPMENT ENVIRONMENT
The circuit configuration employing the Arduino UNO R3 processing board is provided in Figure
5.10. The DIGITAL pins of the microcontroller are used to communicate with the LED interface
circuit. We describe the operation of the LED interface circuit in Chapter 8.
The sketch to implement the project requirements is provided below. As in previous examples,
we define the Arduino UNO R3 pins, set them for output via the setup() function, and write the
loop() function. In this example, the loop() function senses the voltage from the 10K trimmer
potentiometer and illuminates a series of LEDs corresponding to the sensed voltage levels.
//*************************************************************************
#define trim_pot 0 //analog input pin
//digital output pins
//LED indicators0-7
5.8. EXAMPLE: ADC RAIN GAGE INDICATOR 117
220
Vcc = 5 V
10K
2N2222
220
Vcc = 5 V
10K
2N2222
220
Vcc = 5 V
10K
2N2222
220
Vcc = 5 V
10K
2N2222
220
Vcc = 5 V
10K
2N2222
220
Vcc = 5 V
10K
2N2222
220
Vcc = 5 V
10K
2N2222
220
Vcc = 5 V
10K
2N2222
Vcc = 5 V
10K
5V
Gnd
ANALOG IN
012345
3
1
2
1
1
1
0
1
98 76543210
DIGITAL
Arduino
UNO R3
Figure 5.10: ADC with rain gage level indicator.
118 5. ANALOG TO DIGITAL CONVERSION (ADC)
#define LED0 0 //digital pin
#define LED1 1 //digital pin
#define LED2 2 //digital pin
#define LED3 3 //digital pin
#define LED4 4 //digital pin
#define LED5 5 //digital pin
#define LED6 6 //digital pin
#define LED7 7 //digital pin
int trim_pot_reading; //declare variable for trim pot
void setup()
{
//LED indicators - wall detectors
pinMode(LED0, OUTPUT); //configure pin 0 for digital output
pinMode(LED1, OUTPUT); //configure pin 1 for digital output
pinMode(LED2, OUTPUT); //configure pin 2 for digital output
pinMode(LED3, OUTPUT); //configure pin 3 for digital output
pinMode(LED4, OUTPUT); //configure pin 4 for digital output
pinMode(LED5, OUTPUT); //configure pin 5 for digital output
pinMode(LED6, OUTPUT); //configure pin 6 for digital output
pinMode(LED7, OUTPUT); //configure pin 7 for digital output
}
void loop()
{
//read analog output from trim pot
trim_pot_reading = analogRead(trim_pot);
if(trim_pot_reading < 128)
{
digitalWrite(LED0, HIGH);
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
digitalWrite(LED6, LOW);
5.8. EXAMPLE: ADC RAIN GAGE INDICATOR 119
digitalWrite(LED7, LOW);
}
else if(trim_pot_reading < 256)
{
digitalWrite(LED0, HIGH);
digitalWrite(LED1, HIGH);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
digitalWrite(LED6, LOW);
digitalWrite(LED7, LOW);
}
else if(trim_pot_reading < 384)
{
digitalWrite(LED0, HIGH);
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
digitalWrite(LED6, LOW);
digitalWrite(LED7, LOW);
}
else if(trim_pot_reading < 512)
{
digitalWrite(LED0, HIGH);
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
digitalWrite(LED6, LOW);
digitalWrite(LED7, LOW);
}
else if(trim_pot_reading < 640)
{
digitalWrite(LED0, HIGH);
digitalWrite(LED1, HIGH);
120 5. ANALOG TO DIGITAL CONVERSION (ADC)
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED5, LOW);
digitalWrite(LED6, LOW);
digitalWrite(LED7, LOW);
}
else if(trim_pot_reading < 768)
{
digitalWrite(LED0, HIGH);
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED5, HIGH);
digitalWrite(LED6, LOW);
digitalWrite(LED7, LOW);
}
else if(trim_pot_reading < 896)
{
digitalWrite(LED0, HIGH);
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED5, HIGH);
digitalWrite(LED6, HIGH);
digitalWrite(LED7, LOW);
}
else
{
digitalWrite(LED0, HIGH);
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED5, HIGH);
digitalWrite(LED6, HIGH);

Get Arduino Microcontroller, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.