Analog Read
1. Objective
The objective is talk about, how to use analog pin read signal for potentiometer to control digital pin arduino.
2. Requirement
- Arduino Uno x1
- USB Cable x1 (No need in simulation)
- Breadboard x1 (No need in simulation)
- Potentiometer x1 (Name in Proteus is POT-HG)
- LED x3
- Resistor (220Ohm or 330Ohm) x3
- Electronic Wire (No need in simulation)
- Virtual Terminal (for monitor in Proteus)
3. Build Circuit
Build circuit like Figure 1.
Figure 1. Arduino Connection |
4. Coding
// By Tann Thona
// 0V = 0;
// 5V = 1023
// x = (3*1023)/5 = 613.8
int val = 0;
void setup() {
// put your setup code here, to run once:
pinMode(A0, INPUT);
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
val = analogRead(A0);
if(val <= 200)
{
digitalWrite(13, 1);
digitalWrite(12, 0);
digitalWrite(11, 0);
}
else if(val <= 512)
{
digitalWrite(13, 0);
digitalWrite(12, 1);
digitalWrite(11, 0);
}
else if(val <= 1023)
{
digitalWrite(13, 0);
digitalWrite(12, 0);
digitalWrite(11, 1);
}
Serial.println(val);
delay(10);
}
Click Here to Download Code and Proteus Simulation File
4. Result
According to code above, this two LEDs was switching bright depend on potentiometer vary resistance.Watch Video Explain in Khmer
YouTube: ICT4D-KH
Copyright By Tann Thona
No comments:
Post a Comment