0

I’m using an Arduino-uno

I'm trying to read an PWM signal being output by pin 6. I'm attempting to read it with pin A0.

5 volts are being supplied from pin 6 to a 300 ohm resister in series with one led. This circuit returns to the analog input pin A0.

It'll admit it's a strange set up and I was told to use a pass through filter to read the signal.

At this point I'm not concerned with whether the program reads the right values, so despite the code involving the serial monitor I'm just trying to figure out why the led won't turn on anymore.

As I stated in the sketch, this exact set up was working fine yesterday. The led was on and (accurate or not) there were consistent values being printed to the serial monitor.

I'm trying to figure out now what changed after I unplugged it last to create a now open circuit.


```

void setup() {

  Serial.begin(9600);
  pinMode(6, OUTPUT);
  // I  also tried setting the A0 pin as an input even though I thought you didn't have to and it didn't change anything.

}

void loop() {


  analogWrite(6, 255); // This pin is working as an output, I tested it to ground and led lights.

  int value = analogRead(A0); // I realize inputs are typically used for things like potentiometers
                              // But just as an example the LED should still turn on when connected here, correct?
                              //And it did the first few times I did it... maybe that was the problem?

  Serial.println(value);  // This was  all working previously. It just randomly stopped.
                          // Now this just prints random numbers and the circuit remains incomplete.
                          // Even if the input pins are not supposed to be used with 5 volts, Why was it working before then...
                          // ALSO it does not matter which pins you use for what...
                          // No current is running through the digital inputs with digital output set to HIGH.
                          // And No current runs through the analogInput pins when analogWrite is set to 255.
                          // Once again, I apologize if my terminology is off.


             

}
```

This is what I did to trouble shoot:

With the same sketch running on the board...

I connected a jumper from pin A0 to ground and the serial monitor switched from a bunch of random values to a solid 0. So it recognizes input still... in a sense.

Then I modified the sketch...

void setup() {

Serial.begin(9600);
pinMode(6, OUTPUT);
pinMode(8, INPUT);

}

void loop() {

// now I'm attempting to read a digital signal with the same circuit...
// Pin 6 is connected to a 300 ohm resistor in series with an led which returns to pin 8.


digitalWrite(6, HIGH); 

int value = digitalRead(8);

Serial.println(value);

}

This gave the same result. The led wont turn on anymore unless the circuit ends are connected from a voltage source to ground.

Previously, any pins configured as INPUT would not only read consistent values but also complete the circuit, turning the led on.

Now the pins only recognize zero volts and do not complete any circuits. You can't even use a simple potentiometer if you wanted.

Each pin that can be configured as an INPUT acts the same way.

The only thing I did between the times when it was working and wasn't was unplug and plug in the board...

13
  • 1
    Sorry for the confusion. I did not expect an input to light an LED, you're right that would be absurd. I figured some assumption could be made. My bad, The post is updated.
    – User-2746
    Oct 5 '20 at 18:42
  • 1
    Yes, connecting the end of the circuit. The other end is hooked up to a pwm source. Besides, right after that I state, "it doesn't work when connected to ground either". So your assumption would also imply that I thought a circuit with absolutely no power source should be able to turn on an led.
    – User-2746
    Oct 5 '20 at 19:45
  • 1
    Sorry, "it does work when connected to ground". See, the original post IS confusing. That is why I updated it. Either way my point stands. It should not be hard for a person like your self to make the right educated assumption to that statement. Instead you assume people's ignorance's, pick out the all the discrepancies, and remain unhelpful. This is why forums are not as popular anymore. If you are genuinely confused and need anymore clarification I can do that, otherwise don't bother posting. It's a waste of time.
    – User-2746
    Oct 5 '20 at 20:13
  • 1
    I don't mean to argue, but I too can not read your mind. What you said already was mentioned by yes, three people and cleared up. And the way you said it would give anyone a reason to assume "unfriendliness" on your end as well. You say you can not read my mind yet you literally state what you believe I am thinking. I was not assuming intelligence, I was assuming tone. Which I still think to be in your words "unfriendly". I respect that you do not want to help. However, respectfully, I do hope you realize the hypocrisy in your respons and just remember that there really was no point in this.
    – User-2746
    Oct 5 '20 at 20:51
  • 2
    Chris, I can do that. I'll update it again in a little bit thanks.
    – User-2746
    Oct 5 '20 at 20:52
7

Sounds like there's a basic misunderstanding here.

Setting a pin as "output" mean it'll either supply +5V ("HIGH") or 0V ("LOW"). Both those states are considered "outputs", and the pin can source or sink current in those states. When driven "LOW", the circuit does "input" current to the chip, but that's not what "input" usually means.

"Input" means you can use the pin to read the voltage applied to the pin. When connected to a PWM source, this might manifest as the pin quickly switching between "1" / "HIGH" / "+5V" and "0" / "LOW" / "0V". Normally, you would use the "input" mode to read things like switches, or potentiometers on the analog input pins.

Your desired use of "current input" pins will require you to configure the pin as "output", and then set it to "LOW", which will make it able to sink current.

5
  • 4
    This seems very much to me to be the correct answer. An input pin does not "suck in current". It measures if it is there or not. As an analogy, a thermometer does not make things hot or cold, it measures if they are hot or cold.
    – Nick Gammon
    Oct 5 '20 at 6:32
  • Sorry for the confusion, I updated the post. I was attempting to use the analog input pins (either A0 - A5) to read the voltage from the circuit which I powered with a pwm source. So where I'd normally connect the end of the circuit to ground, I connected it to an analog input pin configured to "read" the voltage. It was working previously, can this not be done?
    – User-2746
    Oct 5 '20 at 18:37
  • @User-2746 Does that mean you are trying read a PWM sicnal with analogRead()? That will not work as long as you don't use something like a low pass filter.
    – chrisl
    Oct 5 '20 at 19:47
  • Yes, thank you. I will try this. I'm still slightly confused because previously using the same exact set up the led was on. Whether or not the PWM signal was being read properly due to no filter, I'm not sure, but the led it's self was working. It's like something changed from when I unplugged everything to when I tried it again the next day. Because now, with the same set up, the circuit is incomplete when connected from a PWM on pin 6 to the analogRead on pin A0... Which once again is the opposite to what it did originally.
    – User-2746
    Oct 5 '20 at 20:30
  • I also updated it one more time for much needed clarification. It's a strange problem and I didn't explain it well enough.
    – User-2746
    Oct 5 '20 at 21:31
1

The LED would glow only when the pin is configured as an OUTPUT or it would be very dim in INPUT mode. This is because INPUT mode sets the pin to a high impedance state, suitable for reading voltages and not for driving circuits requiring current.

Setting a digital pin to INPUT mode, allows you to only read voltages as 0 or 1 which correspond to 0V and 5V respectively. For example, upload the code below to your Arduino and open the serial monitor. Now, by connecting Pin 11 to GND the serial monitor should display 0, and by connecting it to 5V, it should display 1. If this doesn't work something may be wrong with the Arduino.

const int TESTPIN=11; //connect pin 11 to 5V and GND for testing

void setup() {
  
  pinMode(TESTPIN, INPUT);
  Serial.begin(9600);
}

void loop() {
  Serial.println(digitalRead(TESTPIN));
  delay(500);
}

However, I would recommend that you share your code and diagram before concluding a fault in the Arduino.

2
  • Thanks for the input. I tried what you said and it helped explain the problem a little better. The monitor displayed "0" when connected to ground but the values are all random when supplied with voltage. I think your right, somethings wrong with the board. I updated my post adding this in. It's very strange that it just failed so randomly. It was working at first, then I unplugged it, plugged it back in the next day and now it doesn't.
    – User-2746
    Oct 5 '20 at 21:36
  • Just to confirm, I hope you connected PIN11 directly to 5V using a jumper with nothing in between? Oct 6 '20 at 3:34
0

In your second update you described the following circuit:

schematic

simulate this circuit – Schematic created using CircuitLab

Then in code you are doing digitalWrite(6, HIGH);, which sets pin 6 to HIGH/1/true/5V. Pin 6 is configured as output, pin 8 as input. In this situation the LED will not light up. A pin, that is configured as input, will not draw or source current (except a very very small amount). The input pin behaves somewhat like a little capacitor to ground (so in principle nearly no current can flow). Thus no current is flowing through the circuit. No current also means, that the LED does not light up (it needs current for lighting up) and no voltage drop will happen on the circuit. The circuit will not be "completed", as you phrased it. Thus your input pin will also get HIGH/1/true/5V, which is what you then read on in the code.

When you instead change pin 8 to output (pinMode(8, OUTPUT);) and set it to LOW (digitalWrite(8, LOW);), the LED will light up, as now the voltage difference is the full 5V (from HIGH at pin 6 to LOW/ground at pin 8). If you now read the pin with digitalRead(8) you will just read, what you have written to it, meaning LOW/0/false/ground.

You might also have tried to use digitalWrite() with a pin, which is configured as INPUT. That does not really output anything. Instead it activates or deactivates the internal pullup resistor of the pin. As I remember, this pullup resistor is between 20 and 50 kΩ, so again you cannot get enough current to light an LED through that pin. The internal pullup resistor is then like an additional 20 to 50 kΩ resistor from the corresponding input pin to Vcc (5V in your case). If nothing else pulls on the pins value, the pullup resistor will pull the value of the input pin to HIGH/1/true/5V, so that you don't have a floating pin (see below for an explanation of that term).


The next scenario: Using analogRead()

Here you first tried to read an analog pin (A0), which was not connected to anything. That is called a floating pin. There is nothing, that gives this pin a definite value, thus it catches whatever electrical noise comes around, from the microcontroller, from other pins, from the circuit or from outside. A floating pin can change it's value rapidly and randomly, depending on the noise, which comes around. Thus you will get very different and varying values here.

Then you connected A0 to ground. That gives the pin a definite value (LOW/0/false/ground). Thus you now only read zero as value.

If you are doing the analogRead() with a circuit similar to the first case (with the LED between two pins) you will get a similar result as with digitalRead(). An analog input will also not source or draw any current (besides a very very small current). Thus you cannot "complete" a circuit to light an LED on the path.


I'm trying to read an PWM signal being output by pin 6. I'm attempting to read it with pin A0.

A PWM signal is nothing else than changing rapidly between Vcc (5V in your case) and ground. The value, that you give the PWM signal via analogWrite(), defines the duty cycle of the signal, meaning the ratio between the HIGH and LOW portions in the signal. The function name analogWrite() is unfortunately very misleading, as you don't really output an analog voltage. Most Arduinos don't have the hardware to do that, so analogWrite() uses PWM to output a pseudo analog voltage. Devices like motors are not fast enough to react to the fast voltage change, so you can change the speed with PWM. LEDs are fast enough, but the human eye cannot see the individual on-off cycles, so you see the LED being dimmed with lower values.

But the analogRead() function (which uses the ADC hardware internally) is fast enough, thus you would only read a series of changes between 5V and ground, when reading a PWM signal. You could build a low pass filter, which will filter out the PWM frequency and give you a real analog voltage, which you can then read via analogRead(). Though keep in mind, that this makes it slower, so you cannot read fast changes with that. A low pass filter looks like this:

schematic

simulate this circuit

The values come from the come from the comment of Edgar Bonet (thanks):

The PWM frequencies of the Uno are 977 and 490 Hz, below the cut-off of the filter you drew. Per the ADC's specification, you can safely increase R1 up to 10 kΩ, which should put the cutoff low enough to attenuate the PWM frequencies by at least 30 dB.

Though you might also want to read up the formula for the cut-off frequency. You can google terms like "RC low pass filter".

If you don't want to use extra hardware, but still want to read the PWM signal, you need to read the pulse length. For this there are multiple ways, depending on the requirements. You can use pulseIn(), you can use interrupts and take timestamps, you can use the capture mode of a hardware Timer.


As I stated in the sketch, this exact set up was working fine yesterday. The led was on and (accurate or not) there were consistent values being printed to the serial monitor.

Unfortunately this is not possible. The pins must behave like I described above. As electronics not break and then just magically unbreak, you must have done something different. An input pin (digital or analog) will not source or draw enough current to light an LED.


Now the pins only recognize zero volts and do not complete any circuits. You can't even use a simple potentiometer if you wanted.

You may have damaged something, though I cannot really say, if that is really the case or if you are just doing something wrong there. A potentiometer has to be connected like the following: The outer pins to ground and 5V respectively, the middle pin directly to the analog input. Nothing else. You might check, if that is, what you did.


Besides all that, you didn't really state, what you are trying to achieve. As you already acknowledged in your question, the described circuit is not typical and a strange setup. To light an LED, you would just connect it to the output pin (via the resistor) and on the other side to ground.

3
  • Nice answer! Just two comments: 1. The pullup is specified to be between 20 and 50 kΩ (not MΩ), with 30 kΩ being typical. 2. The PWM frequencies of the Uno are 977 and 490 Hz, below the cut-off of the filter you drew. Per the ADC's specification, you can safely increase R1 up to 10 kΩ, which should put the cutoff low enough to attenuate the PWM frequencies by at least 30 dB. Oct 6 '20 at 9:47
  • @EdgarBonet Ups, thanks for correcting. Thinking back, 50MOhm should have made me suspicious of my claim XD. I have included the 10kOhm value into the schematics and mentioned/cited you in the answer.
    – chrisl
    Oct 6 '20 at 14:11
  • I got here with a similar problem to the OP, I see they didn't answer your question about what they wanted to achieve, I can't figure it out but I can explain what I was trying to do. I have a toggle swtich on-none-on, and I want to have 2 leds, to indicate which one is on (yes I know I could just look at the switch :) ). What I wanted to do was have the LEDs in-line with the input to the Arduino so that instead of using 2 pins for inputs from the switch and 2 pins as outputs for the LED's I could get away with just using the 2 input pins. Jul 11 at 13:44

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.