analog write pin

Play with it! Please tell us which board you're using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
GuitarHero
 
Posts: 40
Joined: Fri Apr 06, 2018 10:01 am

analog write pin

Post by GuitarHero »

Hello,

I'm trying to set the pin A0 to a certain DC Level, but it doesn't work.
I'm expecting the A0 pin to generate a constant DC voltage of 0-3,3V, depending on the value 0-1023 of the analogWrite() method.

Below is my example code.
The unexpected result is: A0 does NOT output 3,3V and the LEDs don't even blink until I remove the call to analogWrite().
What am I doing wrong?

Code: Select all

let heartbeat = false
forever(function () {
    pins.A0.analogWrite(1023)
    heartbeat = !(heartbeat)
    if (heartbeat) {
        light.setAll(0xffff00)
    } else {
        light.setAll(0x000000)
    }
    pause(500)
})

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: analog write pin

Post by adafruit_support_bill »

Analog input pins measure analog values, they do not generate them.

The "AnalogWrite" function works only on PWM pins. On an Arduino UNO, that would be digital pins: 3, 5, 6, 9, 10, and 11.

The "Analog" output is not an analog voltage. It is a PWM signal modulated between 0% and 100% duty cycle.
https://www.arduino.cc/reference/en/lan ... alogwrite/
https://www.arduino.cc/en/Tutorial/PWM

User avatar
GuitarHero
 
Posts: 40
Joined: Fri Apr 06, 2018 10:01 am

Re: analog write pin

Post by GuitarHero »

I'm not talking about an Arduino, I'm using a Circuit Playground Express, the documentation says:

A0 (a.k.a D12) - This is a special pin that can do true analog output so it's great for playing audio clips. In can be digital I/O, or analog I/O, but if you do that it will interfere with the built-in speaker. This is the one pin that cannot be used for capacitive touch.

So I thought "true analog output" means I can set a constant analog DC output voltage with the function below:
analog-write.png
analog-write.png (3.22 KiB) Viewed 655 times

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: analog write pin

Post by adafruit_support_bill »

Sorry, I didn't realize you were using the 'Express' version of the Circuit Playground. That one does have a DAC connected to A0.

Are you setting the analog write resolution to 10 bits? I believe it defaults to 8 bits and I'm not sure what it would do with a 10 bit value in that case.
https://www.arduino.cc/reference/en/lan ... esolution/

User avatar
GuitarHero
 
Posts: 40
Joined: Fri Apr 06, 2018 10:01 am

Re: analog write pin

Post by GuitarHero »

adafruit_support_bill wrote:Sorry, I didn't realize you were using the 'Express' version of the Circuit Playground. That one does have a DAC connected to A0.

Are you setting the analog write resolution to 10 bits? I believe it defaults to 8 bits and I'm not sure what it would do with a 10 bit value in that case.
https://www.arduino.cc/reference/en/lan ... esolution/
No, I don't.
The code above is everything I do.

My value is 1023 (‭binary 11 1111 1111‬ = 10 Bits), however the documentation for analogWrite() says:
"You are setting the amount of signal at the pin. You use values between 0 (for no signal) and 1023 (for full signal)."

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: analog write pin

Post by adafruit_support_bill »

Let me check with some of the MakeCode experts . . .

User avatar
adafruit2
 
Posts: 22148
Joined: Fri Mar 11, 2005 7:36 pm

Re: analog write pin

Post by adafruit2 »

ok tried this. on A1 pad, which has PWM support, you can create a PWM square wave. but on A0 it doesnt work as PWM or analog.
A0 is sort of reserved for audio/beeping. so that could be part of it. ill open an issue!

User avatar
adafruit2
 
Posts: 22148
Joined: Fri Mar 11, 2005 7:36 pm

Re: analog write pin

Post by adafruit2 »

makecode peeps suggest https://makecode.adafruit.com/beta !

User avatar
andrewa
 
Posts: 145
Joined: Mon Oct 19, 2009 9:53 pm

Re: analog write pin

Post by andrewa »

As far as the true analog output goes on the Circuit Playground Express, it seems relatively simple.

One of the Neopixels lights up, full white; this even happens when a null program is loaded. Any clues on why this happens?

Code: Select all


int sensorVal = 0;
int outputVal=0;
int increment = 1;
int upperlimit;
int lowerlimit;
int resolution;

void setup() {
  resolution = 12;
  analogWriteResolution(resolution);
  upperlimit = pow(2, resolution) - 1;
  lowerlimit = 0;
  Serial.begin(9600);
  delay(1000);

  Serial.print("resolution = ");
  Serial.print(resolution);
  Serial.print(" upperlimit = ");
  Serial.print(upperlimit);
  Serial.print("\n");
  while(millis()<5000);
  {
    
  }
}

void loop() {

    sensorVal = sensorVal + increment;

    if (sensorVal >= upperlimit)
    {
      increment = -1;
    }

    if (sensorVal <= lowerlimit)
    {
      increment = 1;
    }

    outputVal=sensorVal;


  // ensure you never are out of range
  outputVal = constrain(outputVal, 0, upperlimit);

  Serial.println(outputVal);

  analogWrite(A0, outputVal);

}

User avatar
adafruit2
 
Posts: 22148
Joined: Fri Mar 11, 2005 7:36 pm

Re: analog write pin

Post by adafruit2 »

the neopixel pin is floating, so some noise can come in, make it blink. use the CPX library to create the neopixel object and initialize it, this will clear out the pixels and keep the pin low

User avatar
kevinjwalters
 
Posts: 1025
Joined: Sun Oct 01, 2017 3:15 pm

Re: analog write pin

Post by kevinjwalters »

Did you get this working in MakeCode? I've just tried to use it sometime after this thread was started and it doesn't work for me, details are in analog write errors on Circuit Playground Express A0 (true analogue) #1061.

Locked
Please be positive and constructive with your questions and comments.

Return to “Circuit Playground Classic, Circuit Playground Express, Circuit Playground Bluefruit”