I modified the code (see attached) in Miuzei Lesson 10 to read the value on the redPin using rr=analogRead(redPin) and println(rr) it to the serial monitor.
The relevant point in this lesson is that a RGB LED sequentially goes from one color to another; red is on, then it is off while green and blue light up. I expected the analogRead of redPin to be large when the red color showed, and zero, or at least a small number, when the green and blue colors showed. To my surprise, the analogRead(redPin) value never changed significantly!
However, when I changed rr=analogRead(redPin) to rr=digitalRead(redPin) I observed 1’s and 0’s exactly where I expected them to be.
The sketch attached prints both the digital (rrd) and analog (rra) values to the serial monitor. Why doesn’t analogRead(redPin) follow the activity of the red pin, yet digitalRead(redPin) does?
RGB_LED.ino (999 Bytes)
Please post your code using code tags rather than attaching it. It makes it easier for people to help you.
Your redPin variable points to a digital pin, you are not reading it if you use analogRead(). Those only apply to A0..A7
int redPin = 11;
rra = analogRead(redPin);
You are doing an analogRead() of a digital pin. Does that seem right to you ?
You can only use "analogRead()" on analog input pins (A0 =14 through A5 =19 on an Arduino UNO). You can only use analogWrite() on PWM pins (3, 5, 6, 9, 10, 11 on an Arduino UNO). There is no overlap in those two sets so you can't analogWrite() and analogRead() the same pin. Even if you could, analogWrite() does PWM: Pulse Width Modulation. Tha output is not a voltage but a 5V signal that is either on or off. The portion of time the signal is on is controlled by the analogWrite() value. Zero means 'never on', 128 means on half the time, and 255 means on all the time. If you connect an analogWrite() pin to an analogRead() pin and set the analogWrite() to 128 the analogRead() should come back 1023 (5V) half the time and 0 (0V) half the time.
Even though they're both called analog, the analogRead and analogWrite functions are not remotely related to each other and use completely different concepts and hardware. Importantly, analogWrite does not create a true analog output. It uses Pulse Width Modulation and can be used to mimic an analog output for certain types of circuits (like LEDs).
Many thanks to all who replied; I learned a lot. I'm going through the lessons one by one, and I'm still flying by the seat of my pants. I appreciate very much having the folks on the forum as my copilots!