-1

I can't figure out why the output voltage of analogWrite(255) is less than the voltage of digitalWrite(255). My code and an image of my setup are below. Some other useful information is that I am using the NodeMCU by HiLetgo, 3.3k ohm resistors with the meters, and that the meters use 1mA of current DC at full scale. Thanks for your help!

int pPressure       = D2;
int pPrecipProb     = D1;
int pWindSpeed      = D0;

int mTemperature;
int mHumidity;
int mPressure;
int mPrecipProb;
int mWindSpeed;
int mAlert;

void setup() {
  Serial.begin(9600);
  pinMode(pPrecipProb,   OUTPUT);
  pinMode(pWindSpeed,    OUTPUT);
  pinMode(pPressure,     OUTPUT);
  pinMode(LED_BUILTIN,   OUTPUT);     // Initialize the LED_BUILTIN pin as an output
}

// the loop function runs over and over again forever
void loop() {
    mPressure           = (int) constrain(mPressure + 1, 0, 255);
    mPrecipProb         = (int) constrain(mPrecipProb + 1,     0, 255 );
    mWindSpeed          = (int) constrain(mWindSpeed + 1,     0, 255 );

    if(mPressure == 255)
    {
      mPressure = 0;
      mPrecipProb = 0;
      mWindSpeed = 0;
    }

    digitalWrite(pPressure,      HIGH   );
    analogWrite(pPrecipProb,    255 );
    analogWrite(pWindSpeed,     mWindSpeed  );


    delay(10);                    

}

Left

Left: AnalogWrite(255)

Middle: DigitalWrite(HIGH)

Right: the other one that cycles

  • 1
    Have you tried swapping the meters/resistors? – Ignacio Vazquez-Abrams Jun 8 '16 at 0:44
  • 2
    Those analog meters may be loading (drawing enough current to cause the voltage to droop) the "analog" output. The digital output likely has a higher drive ability. I don't know this for sure, so I'm not putting it as an answer. – slightlynybbled Jun 8 '16 at 0:57
  • @slightlynybbled: analogWrite() on a ESP8266 performs (software) PWM on a normal digital output. – Ignacio Vazquez-Abrams Jun 8 '16 at 1:12
6

First, do something simpler, and eliminate possible causes.

  1. Are you certain the NodeMCU's CPU is an 8bit PWM? If it were 9bits, it would read about 50% full scale, and 10bits would be about 25% of digitalWrite (which is what I think I can see on the photo).
  2. Swap the analogue meters around, and see if the difference is consistent with the pin, or consistent with the meters.
  3. Use a digital multimeter (DMM) to measure voltage. It should be a small load. So this should eliminate, or identify, the analogue meters as a culprit.

If there is still a difference in voltage, do the NodeMCU equivalent of:

void setup() {
  pinMode(10, OUTPUT);  // a pin capable of analogOut and DigitalOut
}

void loop() {
  digitalWrite(10, HIGH);
  analogWrite(9, 255);  // a pin capable of analogOut and DigitalOut

  delay(100);
}

Then swap 9 and 10.

If the DMM shows the analogue output is consistently lower, then go back and check that PWM is 8 bits, and the device output range for PWM is the same output voltage as digitalWrite to a GPIO.

  • 2
    ESP8266's default PWM resolution is actually 10 bits, which would be consistent with the 25% output shown in the picture. – Ignacio Vazquez-Abrams Jun 8 '16 at 1:15
  • @IgnacioVazquez-Abrams - Ah ha! That was my main suspicion, but I haven't seen a proper data sheet for it. – gbulmer Jun 8 '16 at 1:19
  • Eh, the ESP8266 datasheet isn't particularly enlightening. PWM has to be done in software so you'd have to look at the core documentation instead. – Ignacio Vazquez-Abrams Jun 8 '16 at 1:21
  • @IgnacioVazquez-Abrams - thank you, that clarifies things. I thought I had read that all (most?) pins could supply PWM, but I hadn't realised it was soft. So I was looking for the wrong information. – gbulmer Jun 8 '16 at 1:23
  • 1
    The 10 bit solution worked! I simply switched out the 255 with 1023. Thanks for your help! – user3242816 Jun 8 '16 at 1:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.