analogWrite causes analogRead to return 0

A noob here so please bear with me...

Using the code below can read the potValue until changing the digitalWrite to analogWrite (see comments):

// simple LED with POT to adjust brightness (10k resistor and POT)

int ledPin = 3;
int potPin = A1;
int potValue, outputValue;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  pinMode(potPin, INPUT);
  potValue = analogRead(potPin);
  outputValue = map(potValue, 0, 1023, 0, 255);

  Serial.print("Start: ");
  Serial.print(potValue);  
  Serial.print(",");
  Serial.println(outputValue);  
}

void loop() {
  delay(10000); // temporary to see wierdness
  
  potValue = analogRead(potPin);   // <---- First read reports 0 when void setup () did initialize higher value
  outputValue = map(potValue, 0, 1023, 0, 255);

  Serial.print("Before: ");
  Serial.print(potValue);  
  Serial.print(",");
  Serial.println(outputValue);  

  digitalWrite(ledPin, HIGH);  // When using analogWrite(ledPin, outputValue); potValue is always zero BUT works with digitalWrite (ledPin is PWM) 
  potValue = analogRead(potPin);
  outputValue = map(potValue, 0, 1023, 0, 255);

  Serial.print("After: ");
  Serial.print(potValue);  
  Serial.print(",");
  Serial.println(outputValue); 

  delay(potValue);

}

This is on an UNO board.

I may be missing something obvious but would appreciate guidance to get started.

  • Welcome to the group.

  • Always show us a good schematic of your proposed circuit.
    Show us good images of your ‘actual’ wiring.
    Give links to components.

Do you want to see that the brightness of your LED gradually changes as you rotate the potentiometer?

Yes. Thank you. That is correct. Pot is connected to A1 and reads value correctly. Led is on Pin 3 (PWN capable) and displays correctly when using digitalWrite(ledPin, HIGH)

Please post a couple of pages of serial monitor output, one grabbed when digitalWrite() is being used, and the other when analogWrite() is being used. Post each batch of serial monitor output in its own set of code tags.

If strange things are happening when "nothing" is happening during the delay(), then I suspect wiring. If you are using a solderless breadboard, the connections may be rusted.

  • You have not wired things properly, 5v needs to go to your circuit.

  • It should be similar to this, change pin numbers as necessary.

This is the serial output using analogWrite():

This is with digitalWrite() - no other changes:

Your sketch is moderated as follows: (I assume that you have a 1k/2k series resistor with LED.) upload the sketch, slowly rotate the pot, and chcek that the brightness of LED chnages.

// simple LED with POT to adjust brightness (10k resistor and POT)

int ledPin = 3;
int potPin = A1;
int potValue, outputValue;

void setup() 
{
  Serial.begin(9600);
  Serial.println("Starting: ");
}

void loop() 
{
  potValue = analogRead(potPin);   // <---- First read reports 0 when void setup 
  outputValue = map(potValue, 0, 1023, 0, 255);
  digitalWrite(ledPin, outputValue);  // When using analogWrite(ledPin, 

 delay(1000); 
}

Thank You.

Could I enquire about most ideal free diagramming tool (Windows) ?




  • Fritzing, however, it is not a proper program for making schematics and not liked on this forum.

https://fritzing.org/

1 Like
  • This would be the schematic format that you should strive for.

1 Like

Without delay();

// only print when value potentiometer changes

byte ledPin = 3;
byte potentiometerPin = A1;
int oldPotVal;
unsigned long timer = 0, oldTime = 0, interval = 100; // reading interval in milliseconds

void setup() {
  Serial.begin(115200);
}

void loop() {
  readPotentiometer(); // read the sensor without using delay();
}

void readPotentiometer() {
  int newPotVal = analogRead(potentiometerPin);
  if (millis() - oldTime > interval) { // compare event duration/difference to interval
    oldTime = millis(); // store the new event start
    if (newPotVal != oldPotVal) {
      analogWrite(ledPin, map(newPotVal, 0, 1023, 0, 255)); // PWM for LED
      Serial.print("D:"); // show the reading
      pad(newPotVal); // show the reading
      Serial.print(" A:"); // show the reading
      pad(map(newPotVal, 0, 1023, 0, 255)); // show the reading
      Serial.println();
      oldPotVal = newPotVal;
    }
  }
}

void pad(int value) {
  if (value < 1000) Serial.print(" ");
  if (value < 100) Serial.print(" ");
  if (value < 10) Serial.print(" ");
  Serial.print(value);
}

This is incorrect advice.

You did not follow my advice about how to post that output.

I see now you wanted then in code tags and I put them as images. Noted for next time. For now the issue is resolved by correct wiring. The behavior shown with analogWrite is unexpected since it works as expected with digitalWrite. However, this should not have worked given the wiring correction. Thanks for taking the time.

This is the schematic/connection diagram (Fig-1) of your project, which is made manually using Microsoft Visio app.

Figure-1:

  • Not advice, an opinion.
    :sunglasses: