I'm a beginner with an Arduino Uno and have a question about Led and potentiometer.
I have the really simple following code :
int Pot = A0;
int Led = A1;
int Val;
int Niv;
void setup(){
Serial.begin(9600);
pinMode(Led,OUTPUT);
digitalWrite(Led,LOW);
}
void loop(){
Val = analogRead(Pot);
Niv = Val / 4;
Serial.println(Niv);
analogWrite(Led,Niv);
delay(50);
}
My potentiometer gives information through A0, and my red led is controlled through A1.
When I trace my "Niv" variable, i get a correct value between 0 and 255.
My problem is I don't understand why my red led is only lightening from 128 to 255 with no change in brightness... and stays off from 0 to 128 !
My problem is I don't understand why my red led is only lightening from 128 to 255 with no change in brightness.
Because the pin you have connected to is not capable of PWM, it is not an analogue output.
On the Arduino PWM enabled pins are marked with a ~ on a UNO these are pins 11, 10, 9, 6, 5 & 3. Use one of those pins.
Hi! Here is a bit harder example, but its working. All u need are 3 LEDs on pins 42, 52 and 30, potentiometer, of course arduino and few wires. Have fun!
void setup() {
pinMode(42, OUTPUT); //defining pins
pinMode(52, OUTPUT); //defining pins
pinMode(30, OUTPUT); //defining pins
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(A0); //reading from potentiometer
float voltage = sensorValue * (5.0 / 1023.0); //defining sensor value
Serial.println(voltage); //printing voltage in Serial monitor
//now how LEDs are turning on
if (voltage<1) digitalWrite(52, HIGH);
if (voltage>1) digitalWrite(52, LOW);
if (voltage<1) digitalWrite(42, LOW);
if (voltage>1, voltage<4) digitalWrite(42, HIGH);
if (voltage>4) digitalWrite(42, LOW);
if (voltage>4) digitalWrite(30, HIGH);
if (voltage<4) digitalWrite(30, LOW);
if (voltage<1) digitalWrite(42, LOW);
}