How to use analogWrite

Post here first, or if you can't find a relevant section!
Post Reply
forfrends
Posts: 14
Joined: Mon Jan 20, 2020 10:49 am

How to use analogWrite

Post by forfrends »

Hello!
Tell me how to do analogWrite correctly? I use simple code, but it does not work, the LED just turns on and off:

Code: Select all

const int thisPin = PC13;

void setup() {
    pinMode(thisPin, OUTPUT);
}

void loop() {
    for (int brightness = 0; brightness < 65536; brightness++) {
      analogWrite(thisPin, brightness);
      delay(10);
    }
}
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: How to use analogWrite

Post by fpiSTM »

Don't know which board you use but the PC13 must have PWM capabilities.
Moreover the analogWrite has a default resolution so using 65535 values is not good, default is 8bits so there is 255 values.
https://www.arduino.cc/reference/en/lan ... alogwrite/
forfrends
Posts: 14
Joined: Mon Jan 20, 2020 10:49 am

Re: How to use analogWrite

Post by forfrends »

I'm sorry, I forgot to indicate board. I am using STM32F103S8T6 (Blue Pill). The kernel is using this: https://github.com/stm32duino/Arduino_Core_STM32
For some reason, AnalogWright does not work. I tried like this, the result is the same:

Code: Select all

void setup() {
    pinMode(PC13, OUTPUT);
    analogWrite(PC13, 64);
}

void loop() {
}
The LED either turns on or off. There is no PWM (I checked with an oscilloscope).
forfrends
Posts: 14
Joined: Mon Jan 20, 2020 10:49 am

Re: How to use analogWrite

Post by forfrends »

Hmm ... it’s not clear ... I just replaced PC13 with PA3 (for the test). There is PWM on PA3, but not on PC13. It seems that the PWM core is not configured for all pins.
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: How to use analogWrite

Post by fpiSTM »

forfrends wrote: Wed Jan 22, 2020 7:56 am Hmm ... it’s not clear ... I just replaced PC13 with PA3 (for the test). There is PWM on PA3, but not on PC13. It seems that the PWM core is not configured for all pins.
So if there is no PWM linked to PC13, the analogWrite simply use it as a digitla IO. If the requested value is less than the half of the resolution value it is LOW else HIGH.
Moreover if the pin has DAC capabilities then the DAC is used in prority.
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: How to use analogWrite

Post by ag123 »

i've not really tried stm32 core deep in, but if the speeds isn't too fast. i'd like to suggest one could hook timer interrupts. that would kind of allow one to pwm any pins by doing so using gpio. there are real speed limits with this approach though. it seemed gpio bit banging often has rather low thresholds, but it'd take experiments to find out. so for those needing the speeds, using actual hardware timer pins would be the best approach.
but if one wants 'generic' pwm on any pins, timer interrupts could be a 'work around'
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: How to use analogWrite

Post by fpiSTM »

There is a lot of pins linked to a Timer.
The analogWrite well use the timer if available on the requested pins.
One other solution is to use directly the HardwareTimer api:
https://github.com/stm32duino/wiki/wiki ... er-library

This is what is called by the analogWrite when Timer is available.
To simulate a pwm on a pin without timer, this is the same, use the HardwareTimer librarly. This is done for the servo.

For analogWrite it is also possible to change frequency and resolution:

Code: Select all

/*
 * \brief Set the resolution of analogWrite parameters. Default is 8 bits (range from 0 to 255).
 *
 * \param res
 */
extern void analogWriteResolution(int res);

/*
 * \brief Set the frequency of analogWrite. Default is PWM_FREQUENCY (1000) in Hertz.
 *
 * \param freq
 */
extern void analogWriteFrequency(uint32_t freq);
Post Reply

Return to “General discussion”