I am quite new to Arduino at the moment. I follow on some basic tutorials and start working on a very simple circuit with analog read and analog write functions. In this stage, I can build the circuit and read/write the analog voltages from/to arduino.
However, I want to modify a bit of the circuit I am currently working on in order to test the combinations of both analog read and analog write functions. Basically, what I want to do is that I want to increase the analog output constantly from pin 9 every 2seconds to power up my led. I also want to measure the voltages at the ledPin by using the analog input (e.g. A0). However, I found that due to the analog signal is generated with PWM function, the measured voltage I is always oscillated between zero and some constant values periodically. For example, the output is something like 358,358,0,0,0,358,358,0,0,0,etc... However, the level of the brightness of LED is actually increasing every 2seconds from the function I defined (output range from 0-255, if bigger than 255, then reset to 0). The results are not idea from what I would expect (e.g. should be something like, 0, 256, 512, 768....etc).
Can anyone suggest me what I can do to measure the voltages as I want? I think potentiometer is one possibility, however, I don't want to adjust the voltages manually. Appreciate a lot.
victorAntonio:
found that due to the analog signal is generated with PWM function, the measured voltage I is always oscillated between zero and some constant values periodically. For example, the output is something like 358,358,0,0,0,358,358,0,0,0,etc... However, the level of the brightness of LED is actually increasing every 2seconds from the function I defined (output range from 0-255, if bigger than 255, then reset to 0). The results are not idea from what I would expect (e.g. should be something like, 0, 256, 512, 768....etc).
Can anyone suggest me what I can do to measure the voltages as I want? I think potentiometer is one possibility, however, I don't want to adjust the voltages manually.
High Victor and welcome,
You should not be surprised about this. The name of the Arduino command analogWrite() is confusing to some. No analogue voltage is produced. Instead a PWM signal is produced where the voltage is either 5V or 0V, but the percentage of the time that the signal is 5V can be varied. This happens so fast that the human eye sees a smooth fading of the led. This is a better way to control the brightness of a LED compared to using a truly analogue voltage. LEDs are not very controllable using analogue voltages.
Why do you want to measure the PWM voltage? Why would you want to use a potentiometer and adjust it manually? What is your real aim?
Thanks for your reply. I can understand that the real analog output is actually generated from digital signals with PWM modulation. I just want to verify what I set leads to what I can get. In this case, for example, I initialize the val = 0; In the loop I defined, I set val = val +51; then delay (2000); such that the led can be controlled every 2seconds and I can observe that the brightness of the LED actually changes from dark to bright in five different levels. As you said, maybe it's not practical to control LED in this fashion, however, I want to use LED to simply observe my results (i.e. output voltage from pin 9 changes).
I measured the voltages on the LED with my physical voltage meter, it shows that the voltage actually changes step by step. But the results from the arduino is not as what I expect. I need to connect the LEDpin to another I/O on another arduino for some control algorithms. For example, if the measured voltage on this LED is higher than 1V, I will coordinate other arduinos to react according to this signal.
Multimeters have integrating ADCs which means they will average out PWM signals
rather well and report a 30% PWM signal as 0.3 x Vcc (for instance). Without an
oscilloscope or logic analyser you can't really get to see the PWM signal directly.
MarkT:
Without an oscilloscope or logic analyser you can't really get to see the PWM signal directly.
Well, you can, and this is what Victor is doing, although it is not what he intended. The Arduino's ADC works so fast that it can act like a "Poor man's Oscilloscope"