Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

AnalogWrite on CPX
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

AnalogWrite on CPX

by KathyCeceri on Sat Oct 19, 2019 9:48 pm

I attached three LED Sequins to pins A1, A2, and A3 on the CPX. I want to try dimming them, but analogWrite only allows you to use pins A0, A1, or A2. Typing it in manually in JavaScript doesn't compile.

On the Learning Guide for the CPX (https://learn.adafruit.com/adafruit-circuit-playground-express/pinouts), it says that A3, A6, and A7 also have PWM output, so I thought that meant I could use them as analog outputs.

What am I getting wrong? (I am avoiding using A0 to light an LED because it says it interferes with the speaker.)

KathyCeceri
 
Posts: 40
Joined: Sun Mar 02, 2014 7:30 pm

Re: AnalogWrite on CPX

by johnpark on Mon Oct 21, 2019 2:40 pm

This seems to be a limitation with the mbed pin mappings: https://github.com/lancaster-university ... d/issues/5

johnpark
 
Posts: 767
Joined: Wed Mar 25, 2009 2:15 pm

Re: AnalogWrite on CPX

by V2man on Tue Oct 29, 2019 2:48 pm

I tried to use a digital write to pin A3 for the purpose of making a servo PWM control signal but was unsuccessful.

V2man
 
Posts: 704
Joined: Mon Dec 03, 2018 12:38 am

Re: AnalogWrite on CPX

by V2man on Tue Oct 29, 2019 10:42 pm

The attach Makecode uf2 can used for non-servo PWM applications where the pulse width is greater than 5 ms and period is greater than 100 ms.
Now I can see button A on the CPX changing the pulse width. The duty cycle is very coarse. What I call ratio.
Starts with a waveform: 5ms high, 120ms low.
Press button A once waveform: 95 ms high, 30ms low.
Press button A again waveform: 5ms high, 120ms low.
etc.
The period of the waveform appears to vary slightly. LEDs may flicker. I don't know if it is detectable. I have not tried this with an external LED string.
Maybe you can use something like this for controlling the LED brightness of an external string.

PWMonPinsOtherThanA1nA2a.JPG
PWMonPinsOtherThanA1nA2a.JPG (88.47 KiB) Viewed 224 times
Attachments
circuitplayground-PWMonPinsOtherThanA1nA2a.uf2
(271 KiB) Downloaded 8 times

V2man
 
Posts: 704
Joined: Mon Dec 03, 2018 12:38 am

Re: AnalogWrite on CPX

by V2man on Tue Oct 29, 2019 11:45 pm

A measured duty cycle from 5ms/125ms = 0.04 or 4 % to 95ms/125ms = 0.76 or 76% with the values used.

V2man
 
Posts: 704
Joined: Mon Dec 03, 2018 12:38 am

Re: AnalogWrite on CPX

by KathyCeceri on Wed Oct 30, 2019 10:39 am

Thanks! It's interesting to see how solutions like this work, even though they are way beyond my level. :)

I teach and write guides for beginners and need to keep things as simple as possible. Which is why I love using MakeCode!

KathyCeceri
 
Posts: 40
Joined: Sun Mar 02, 2014 7:30 pm

Re: AnalogWrite on CPX

by V2man on Wed Oct 30, 2019 11:59 am

@ KathyCeceri
Your welcome. I started out just trying to make a digital waveform for servo control on pins other than A1 and A2 but soon found out I could not create the narrow 0.5 ms or 2.5 ms pulse widths for a period of 20 ms that a servo requires. Then I saw your post and increased the pulse widths to greater than 10 ms with a period of 100 ms. A digital waveform can be created by specifying the high and low times using digital write. Providing the means to change those widths is what makes PWM. That's what push button A does. You can see another version of this Makecode uf2 on this post https://forums.adafruit.com/viewtopic.php?f=64&t=157973

V2man
 
Posts: 704
Joined: Mon Dec 03, 2018 12:38 am

Re: AnalogWrite on CPX

by V2man on Wed Oct 30, 2019 5:05 pm

I cleaned it up some more by putting all of operations under the button A click except for the function, PWM_A3 which stays in the forever block. Input state_A is no longer needed in the function block.

PWMonPinsOtherThanA1nA2s2.JPG
PWMonPinsOtherThanA1nA2s2.JPG (85.32 KiB) Viewed 172 times
Attachments
circuitplayground-PWMonPinsOtherThanA1nA2s2.uf2
(273 KiB) Downloaded 7 times

V2man
 
Posts: 704
Joined: Mon Dec 03, 2018 12:38 am

Re: AnalogWrite on CPX

by V2man on Wed Oct 30, 2019 9:39 pm

@ KathyCeceri
I think this is more what you are looking for to control the brightness of an external string of LEDs using PWM.

PWMonPinsOtherThanA1nA2s3.JPG
PWMonPinsOtherThanA1nA2s3.JPG (114.93 KiB) Viewed 162 times
Attachments
circuitplayground-PWMonPinsOtherThanA1nA2s3.uf2
(273.5 KiB) Downloaded 10 times

V2man
 
Posts: 704
Joined: Mon Dec 03, 2018 12:38 am

Please be positive and constructive with your questions and comments.