When I run the code below on an Arduino Uno, the monitor always show 1023 even I changed the amount of resistance through the variable resistor. The result was that the LED was always on (e.g. the brightness never changes). I have checked all the hardware connection and the coding, nothing seems wrong. Could anyone tell how to fix this problem? Thanks, Raymond
/* potentiometer
AnalogReadSerial
Reads an analog input on pin 3, prints the result to the serial monitor.
Attach the center pin of a potentiometer to pin 3, and the outside pins to +5V and ground.
// the setup routine runs once when you press reset:
int potPin=A3;//select the input pin for the potentiometer
int ledPin=13;//select the pin for the LED
int sensorValue=0;
void setup(){
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 3:
sensorValue = analogRead(potPin);
// print out the value you read:
Serial.println(sensorValue,DEC);
sensorValue=sensorValue/4;//convert from 0-1024 to 0-255
analogWrite(ledPin, sensorValue);
delay(150); // delay in between reads for stability
}
Moderator edit: </mark> <mark>[code]</mark> <mark>
Yes, I have wired it to analog pin 3 to read the analog signal. To isolate the problem, I have measured the voltage between 5V pin and the ground. It is not 5V. Is it normal? Again, for the power supply, is the USB supply sufficient? Thanks, Raymond
Isn't it "on" if the PWM value is > 128, not just non-zero?
that's what happens when you post instead of trying it!
exactly as you said
int ledPin=13;//select the pin for the LED
int sensorValue;
void setup()
{
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
for (sensorValue = 0; sensorValue < 256; sensorValue++)
{
Serial.println(sensorValue);
analogWrite(ledPin, sensorValue);
delay(100);
}
}