Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
3 years, 7 months ago.
FRDM K64F: PwmOut vs AnalogOut
Hi everyone! Yesterday I was doing some experimentation with my FRDM K64F board. The exercise consisted in using the analog input of a potentiometer to control the brigthness of an LED. After a few attempts I was finally able to build the circuit and write the code, but a few doubts came out:
1) How is the brightness of the LED controlled? I was able to get different light intensities by using the PwmOut interface. Initially I was thinking that the LED was controlled by the different possible values of voltage of an AnalogOut interface, but I was not able to obtain the result. Using PwmOut everything work fine. Is the LED controlled by a digital pulse train?
2) The tutorial I was using for the experiment (Arduino-based) told me that the analog input I get from the potentiometer was in the range 0-1023, while the output I want to send to the LED to control the brightness level is in the range 0-255 so I have to use a map function to make the proportion. This wasnt the case with my board. After a few tentative change of the parameters I came to believe that both the value I get from the analog input of the potentiometer using .read and the output i send to the LED using .write are in the range 0-1. Is my hypothesis correct or am I missing something?
Thanks for you for any help :)
3 years, 7 months ago.
Regarding brightness: Start here and look at page 3. You'll see the LEDs are connected to their respective CPU pins. Those pins can only provide "high" or "low" out. So yes, a digital pulse train controls them. In this circuit "low" is "on" because of how the circuit is wired. https://developer.mbed.org/media/uploads/GregC/frdm-k64f_sch_reve3.pdf
PWM uses a "duty cycle" factor which is between 0 and 1. So reading the analog input (0-1) and then sending over to the pwm duty cycle has the correct result.
If you chose instead to use a fixed duty cycle of say 0.5, then to alter the brightness you need to change the period. That's where you might read the analog raw value and shift it appropriately to set a period. Fun to experiment with.