We Can Use the On-Board ADC to Convert Number Between 0 and 1023 Rotating Shaft. Here is How to Make LED ON One at a Time Using a Potentiometer With Arduino. That means, as we will turn the shaft of the potentiometer, LEDs will turn on depending upon the position. Actually before this project, one should get used with two basic examples on official Arduino website :
1 2 | https://www.arduino.cc/en/Tutorial/ReadAnalogVoltage https://www.arduino.cc/en/Tutorial/AnalogInOutSerial |
By turning the shaft of the potentiometer, we change the amount of resistance on the center pin of the potentiometer, so as the voltage at the center pin. So the volatage is from 5 volts (except Arduino Due) is close to zero (resistance on the other side is close to 10 kilo Ohm). As we turn the shaft, the voltage at the center pin changes. This voltage is the analog voltage that we read as input. The Arduino microcontroller of the board has n analog-to-digital converter or ADC which reads this changing voltage and converts it to a number between 0 and 1023. In this project, we are simply using that data in programmatic way to make it looking like volume control knobs with LEDs with physical wiring to turn on/off LED.
Circuit and Code to Make LED ON One at a Time Using a Potentiometer
For this project, we will need :
---
Arduino UNO or similar board
LED – FOUR
220ohm resisters – FOUR
Potentiometer – ONE
Breadboard – ONE
Jumper Wires – FEW
Wiring for this project will be like this :
Here is the code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | #define RES 1023 #define AREF 5 int i; void setup() { Serial.begin(9600); pinMode(3, OUTPUT); } void loop() { Serial.print(valToVolt(analogRead(A0))); Serial.print(" Volts, "); Serial.println(analogRead(A0)); delay(250); i = analogRead(A0); if(i > 32 && i < 250) { analogWrite(3,((analogRead(A0)- 23) / 10)); digitalWrite(5,LOW); digitalWrite(9,LOW); digitalWrite(10,LOW); } else if (i > 250 & i < 500) { analogWrite(3,((analogRead(A0)- 23) / 10)); analogWrite(5,((analogRead(A0)- 23) / 10)-250); digitalWrite(9,LOW); digitalWrite(10,LOW); } else if (i > 500 && i < 750) { analogWrite(3,((analogRead(A0)- 23) / 10)); analogWrite(5,((analogRead(A0)- 23) / 10)-250); analogWrite(9,((analogRead(A0)- 23) / 10)-500); digitalWrite(10,LOW); } else if(i > 750) { analogWrite(3,((analogRead(A0)- 23) / 10)); analogWrite(5,((analogRead(A0)- 23) / 10)-250); analogWrite(9,((analogRead(A0)- 23) / 10)-500); analogWrite(10,((analogRead(A0)- 23) / 10)-750); } else { digitalWrite(3, LOW); digitalWrite(5,LOW); digitalWrite(9,LOW); digitalWrite(10,LOW); } } double valToVolt(double val) { return AREF*(val/RES); } |