-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

Glorfindel
  • 571
  • 1
  • 7
  • 18
user3242816
  • 7
  • 1
  • 2

1 Answers1

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.

gbulmer
  • 901
  • 5
  • 6
  • 2
    ESP8266's default PWM resolution is actually [10 bits](https://github.com/esp8266/Arduino/blob/master/doc/reference.md#analog-output), which would be consistent with the 25% output shown in the picture. – Ignacio Vazquez-Abrams Jun 08 '16 at 01:15
  • @IgnacioVazquez-Abrams - Ah ha! That was my main suspicion, but I haven't seen a proper data sheet for it. – gbulmer Jun 08 '16 at 01: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 08 '16 at 01: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 08 '16 at 01:23
  • 1
    The 10 bit solution worked! I simply switched out the 255 with 1023. Thanks for your help! –  Jun 08 '16 at 01:28
  • @user3242816 - You are welcome. If it answers your question, I'd appreciate you accepting my answer. Not only do I get more points ;-), it also marks in the question list that an answer has been accepted, which may help some of the community. I don't know, but web search engines might also pick up the 'answered' flag. However, I'd hope search engines do a good job of indexing ee.se anyway. While editing this, you have. Thank you! – gbulmer Jun 08 '16 at 01:31