Hi,
I want to set PWM parameters like frequency, duty cycle etc on a Seeeduino XIAO. I have tried several libraries that did not work. I am not good in modifying the libraries. I need the PWM at D1/A1. Can you please provide an example for Arduino IDE that works with the Xiao?
Thanks,
Holger
Hi @HGlasmachers, Did you tired the analogWrite() function to generate the PWM?
example;
void setup()
{
pinMode(1,OUTPUT);
}
void loop(){
for(int i=0; i<=255; i++){
analogWrite(1,i);
delay(10);
}
digitalWrite(1,LOW);
}
Output:
Does it offer a wax to modify the PWM frequency? I need to vary it in small steps between 20kHz and 50kHz. This is easily possible with an ESP32 with a standard library without modification. How do I realize this with the XIAO?
Hi HGlasmachers
I’m set D1/A1 as PWM, actually, I just adjust the duty cycle. If you want to vary it between 20kHz to 50kHz. Please follow this formula:
Frequency = 48MHz / x / 255 / 2 = 20kHz to 50kHz (48MHz is XIAO default frequency)
so between the 20kHz to 50kHz, the answer is x = 1.88 to 4.7
in this code, you just need set val = 1.88 to 4.7
int ledPin = 1;
int val = 0;
void setup() {
// pinMode(ledPin, OUTPUT);
}
void loop() {
for (val = 0; val <= 100; val++) { // 100 -> mean the frequency is 941.1Hz
analogWrite(ledPin, val);
delay(10);
}
for (val = 100; val >= 0; val--) {
analogWrite(ledPin, val);
delay(10);
}
}
Wish it help you.
Best regards
Fenyi
Hi, thank you very much for the time you spend in the answer.
Your code varies the duty cycle, not the PWM frequency. The duty cycle gives the HIGH-time / LOW-time relation. It defines how long the PWM-pin is LOW or HIGH within one time period. The PWM-frequency defines, how often the PWM changes its state within one second.
Best regards,
Holger
Hi HGlasmachers
I see.
use this:
pwm(pin, frequency, duty cycle);
then you just calculator by yourself.
int ledPin = 1;
int val;
void setup() {
}
void loop() {
for (val = 0; val <= 1024; val++) { // 100 -> mean the frequency is 941.1Hz
pwm(ledPin, 20000, val);
delay(1);
}
for (val = 1024; val >= 0; val--) {
pwm(ledPin, 20000, val);
delay(1);
}
}
Best regards
Fenyi
Wow, thank you very much. This solved my problem. You can close this thread. You really did a good job, thank you!
To Fenyi
Where did you locate information on the function pwm(pin, frequency, duty cycle) ?
Thanks
Found the file at
user\AppData\Local\Arduino15\packages\Seeeduino\hardware\samd\1.8.1\cores\arduino
NDC-SC
Hi NDC-SC
That file path is right, you just find out the wiring_pwm.cpp, the function is there.
Best regards
Fenyi
Hi,
What is the upper and lower bounds for the duty cycle in this function? I am getting very weird results when trying to specify the duty cycle
The range for the duty cycle is 0 (always low) to 1024 (always high). If you want pulses then 1 is the shortest high going pulse, and 1023 is the shortest low-going pulse. 512 is a 50% square wave.
Unfortunately the pwm(A0, 732, 256); didn’t solve my problem. I want pin A0 on the XIAO SAM232D to output a pwm, not an analog voltage.
On A0 I couldn’t even get analog voltage. However, on A1, A2 and so on I was able to successfully get a square wave by using pwm
function. Maximum frequency is limited by ~47 kHz though (see Generating a 400 kHz square wave)
In case of SAMD21 pin description structure for A0 states that this is not a PWM pin. So, expectedly, pwm
function will not work on A0. Is SAM232D supposed to be different in that regard?
I’ve run this software and it changes the duty cycle but not the frequency. The frequency stays at 900 Hz. I’m trying to change it to 20KHz. I’m using pin 7 for the output.
I am trying to generate two PWM outputs on the XIAO SAMD21, one at 2kHz, the other at 120Hz. By following the example in the comment marked as the solution to this thread I am able to use the pwm function to change the frequency of one output, but only on pin 1. Is there a way to use pwm to generate two PWM outputs at different frequencies, and is there a way to use the function on a different pin than pin 1? I know the function takes a pin as an argument, but when I try it on other pins I am unable to alter the frequency.
Seeed Studio XIAO SAMD21 has 14 PINs, which can be used for 11 digital interfaces, 11 mock interfaces, 10 PWM interfaces (d1-d10), 1 DAC output pin D0, 1 SWD pad interface, 1 I2C interface, 1 SPI interface, 1 UART interface, Serial communication indicator (T/R), Blink light (L) through pin multiplexing. The colors of LEDs(Power,L,RX,TX) are green, yellow, blue and blue. Moreover, Seeed Studio XIAO SAMD21 has a Type-C interface which can supply power and download code. There are two reset button, you can short connect them to reset the board
I am not an expert but i think you are mixing analog and digital
try pin id as A1, A2, A3 etc, or D1, D2, D3 and dont mix analog and digital read write
you dont need to use the term ledPin as an interger try hard coding the pin as A1
@cgwaltney Your solution worked. Changing from using (int) 1 to A1 and (int) 2 to A2 fixed my issue.
Awesome… Teamwork makes the dream work!