cancel
Showing results for 
Search instead for 
Did you mean: 

I am using the microcontroller in the Nucleo L031K6 model. Our lecturer wants to control LEDs that change according to voltage using ADC and HAL library.

H.Portakal
Associate II

A potentiometer is connected to the breadboard, the middle leg of the potentiometer is connected to the adc pin and the other legs are connected to gnd and 5v.

Led (Led Voltage <1V) - Led will be off

Led (1V<Led Voltage <2V) - Led will blink

Led (2V < Led Voltage < 3.3V) - Led will stay on

0693W00000WJdF1QAL.pngI write the codes as in the image, but when it comes to 1V, the codes stop.

7 REPLIES 7
Bubbles
ST Employee

Hi @H.Portakal​,

one of the problems I see in your code is that you convert integer value x to a float value of y without actual need. It's easier for human to read this way, but for the MCU you introduce useless complexity. Remove y and only deal with x.

Also, all three branches contain toggle pin, so they will all blink, at different frequencies. You need to use WritePin to set a specific value.

You can debug the rest.

BR,

J

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

S.Ma
Principal

Try to avoid using float, for exemple use int for coding mV. And enhancing your example in next step would be to use timer pwm output to drive the led so you can dim it like most laptop standby power led dimming up and down....

I do it as our teacher shows, I am new to the course, so I did not understand what you said. Any chance to show it through sample code?

0693W00000WJeoCQAT.png

I do it as our teacher shows, I am new to the course, so I did not understand what you said. Any chance to show it through sample code?

0693W00000WJeoCQAT.png

S.Ma
Principal

Okay, let's say in microvolt

Int Vadc_mV_x1000 = ad x 10000000 x 3 / 4095

If Vadc_mV_x1000 < 1 x 1000000 then...

else

If V... < 2X 1000000 then...

else ....

Something like this as long as the value can be stored within signed 32 bit int quantity.

Understandable ?

In my case, I only use ***_mV

S.Ma
Principal

One more point, the 3.0V can also be accurately measured by the adc internal channel. Maybe it is 3.28V or 2.97V....

And make sure to calibrate the adc before using it.

Hi @H.Portakal​,

your teacher is probably coming from PC programing background. Microcontroller programmers try to avoid such computations. To correctly use ADC you need more than ADC_Start and ADC_GetValue. I suggest you look at the ADC_RegularConversion_Polling example from the STM32L0 CubePackage. It's the basic simple functional example on what to do in 5 easy steps.

Just change the input if needed and add the LED control and you are done.

BR,

J

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.