In a project we need to produce pwm signal as 1kHz. We use Arduino Giga R1 board. According to examples on web, firstly, we tried to use PWM.h and other libraries but in all libraries we get errors (ie: error: 'TCCR1A' was not declared in this scope and other not declared errors). According to these :
Which library or libraries can we use ?
If we do not want to use any libraries, is there any pwm signal generator code for Arduino Giga ?
Thanks a lot for your help...
PS : The library and pwm generator code works good with Arduino Mega. May be these top libraries are not compatible with Arduino Giga
Of course these libraries are not compatible, Arduino Mega and Giga boards uses absolutely different controller families - Mega has AVR mcu, but Giga equipped with STM32 controller.
You need a library especially for Giga board. Or read a MCU datasheet to learn about STM32 timers.
Addition
As I see, the Arduino package for Giga is mbed compatible.
So you don't need any library to change the PWM frequency, you can use standard mbed PwmOut interface for it :
I also needed a 1kHz PWM signal and was curious to see how easy this was, and I can confirm that the mbed method is pretty simple and it works pretty well.
#include <mbed.h>
PinName pin = digitalPinToPinName(D4);
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
mbed::PwmOut* pwm = new mbed::PwmOut(pin);
pwm->period_ms(1); //1kHz
pwm->pulsewidth_us(500);
}
// the loop function runs over and over again forever
void loop() {
//delay(1);
}
I was able to replicate all the above but I am also trying to be able to adjust the PWM frequency based on a continuous analog input (ultimately a PID controller, but for now just a voltage divider with a variable resistor).
When I previously used an Arduino R4 WiFi, we were able to use PwmOut and have it update the frequency of the PWM by having it in void loop. But here it seems, instead of 'updating' the period, it restarts the wave with each cycle of the loop. This then ends up needing a delay which we cannot have...
Any thoughts? Thanks!
Also, can someone explain what this line is saying/how it's structured:
mbed::PwmOut* pwm = new mbed::PwmOut(pin);
#include <mbed.h>
#define READPIN A1 //voltage divider read value (0 to 1023)
float readadc; // value to be read from A1
float myfreq; // desired frequency
float desperiod; //desired period
PinName pin = digitalPinToPinName(D4);
mbed::PwmOut* pwm = new mbed::PwmOut(pin);
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pwm->period(0.0167);
pwm->write(0.5f);
}
// the loop function runs over and over again forever
void loop() {
readadc = analogRead(READPIN);
Serial.println(readadc);
myfreq = 60;
desperiod = 1/myfreq;
pwm->period(desperiod); //1kHz
pwm->write(0.5f);
delay(10000); //this seems to be needed otherwise it just keeps looping and starting the wave over again
}