I can not believe, but either I have 3 buggy boards, or the problem is really systematic:
if you run the code:
void setup() {
// put your setup code here, to run once:
pinMode(4, OUTPUT);
pinMode(10, OUTPUT);
//10 and 5 as well as 5 and 8
analogWrite(4, 0);
analogWrite(10, 255);
lastMs = millis();
}
void loop() {
}
and measure the output on the pin 4, guess what you get? - PWM signal of 255
digitalWrite() works just fine.
the same situation is observed with the pins 5 and 8. Tested on 3 boards already
I would be really thankful for any idea or a work around.
Hi bernie0815,
the spec says that all pins are pwm.
The funny thing is that if I comment out one line, e.g. //writePWM(10, dim);
then pin 4 works just fine. So I assume a hardware/software bug on pin 4.
i have located the problem - it looks like the pins 4 and 10 as well as 5 and 8 are electrically connected. I have adjusted the description of the issue above.
You have not assigned anything to pins 5 and 8, so they are floating (default is Input at High Impedance). Due to unknown paths in the chip, floating pins may or may not drift to particular values, which may (or, maybe, not) be influenced by what’s going on in nearby pins… Or they may “wander” from time to time.
Bottom line: you can’t depend on unassigned pins having any particular value.
Try the following.
Note that the DC values are nominal, assuming the 3.3V regulator gives exactly 3.3 Volts. On the XIAO that I used for this test, it’s 3.40, so the values on the meter are proportionally higher.
// PWM Test Code for SeeedStudio XIAO
// davekw7x
// February, 2022
/*
If you have a 'scope: If you have a DC Voltmeter
Pin PWM Duty Cycle Pin Voltage
---------------------- ---------------
4 0.2 4 0.66
5 0.4 5 1.31
8 0.6 8 1.97
10 0.8 10 2.63
*/
void setup()
{
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(8, OUTPUT);
pinMode(10, OUTPUT);
analogWrite(4, 51);
analogWrite(5, 102);
analogWrite(8, 153);
analogWrite(10, 204);
//lastMs = millis(); // What the heck is this supposed to do: It's not defined in your code.
}
void loop()
{
}
Note, also, that the code you posted isn’t what you actually tested. (It won’t compile, due to the undefined variable lastMs.)
If you have further problems, it might be easier to get help if you paste your entire, exact sketch.
Hi, due to the resource limitation of the chip, some pins are multiplexed to achieve the function, so it will lead to the current situation. My suggestion is that the function pins (I2C SPI…) are not used as analog pins
Citric (and O.P.) You’re right; I’m wrong. My testing was hurried and sloppy.
With a little more care, here’s what I found:
If you analogWrite(4,…) then analogWrite(10,…), the value written to 10 appears on both 4 and 10
If you analogWrite(10,…) then analogWrite(4,…) the value written to 4 appears on both 4 and 10
I am having the same issue. I need to independently control the brightness of eight LEDs. So from what I understand here, I should be using Pins 1, 2, 3, 4, 5, 6, 7, 9 for this, as Pin 0 does not support PWM and 8 and 10 are connected to 5 and 4, respectively. So far so good!
But can I still use Pins 8 and 10 as analog inputs at the same time to read a potentiometer/trimmer? Or will that again interfere with the LEDs on pins 4 and 5?