Half lit LED and voltage on digital pins?

Hi All

Completely new to Arduino and all things prototyping, I have the book “Make: Getting Started with Arduino” and an uno r3

One of the projects in the book is to make a simple LED operate via using pin 7 and digitalRead

I had this working yesterday but have come back to it today and recoded it, but instead of an off LED connected to any of the digital outputs, Im getting a half lit LED until I trigger pin 7 then it goes full brightness, sometimes the led remains on after the trigger is removed sometimes it goes back to half and sometimes it goes off but then it flickers, this even happens to the on board pin 13 led.

Have I blown something? Multimeter is getting .8v/.9v on all un-configured digital pins at all times and upto 2.5v on the LED pin even though it is not triggered.

Thanks

Please Auto format your code in the IDE, select all of it, use Copy for forum in the IDE and paste it here so that it is code tags to make it easier to read and copy for examination

Post the code that you uploaded to the Uno.

Post a schematic of your wiring. Include all components and power supplies.

Post a complete wiring. That can show things.

I bet that a pinMode(ledPin,OUTPUT) is missing. :nerd_face:

Hello Pete, welcome.

The folk here answering questions are probably not familiar with the project book you are using so won’t know what exactly you are doing, which is why you are being asked for more details. The cause of the problem will be some very specific detail.

Although I have tried this with an external LED and resistor, the issue is there on the internal LED on pin 13, so the only wiring is from 5v to pin 7.

const int LED=12;
const int BUTTON = 7;
int val =0;

void setup() {
pinMode (LED,OUTPUT);
pinMode (BUTTON,INPUT);

}

void loop() {

val = digitalRead (BUTTON);

 if (val == HIGH)
 {digitalWrite (LED,HIGH);}

else {digitalWrite(LED,LOW);

}}

That said I’ve just double checked the wiring diagram in the book and there is a resistor between pin 7 and GND, i’ve inserted that and it works. I don’t know what that is there for but that’s something to figure out I guess!

Thanks for your replys they made me look at it again instead of assuming the uno was broke.

It’s called a pull-down resistor. It’s there to make the pin read LOW when it is not connected to +5V. Without it, the pin will ‘float’ and produce random results.

The Arduino UNO/NANO/MINI/MEGA/Leonardo/Micro have built-in pull-up resistors you can enable:
pinMode(BUTTON, INPUT_PULLUP);
If you use it, you don’t need the external resistor. You, just need a button between the pin and Ground.

On almost all Arduinos, HIGH==true==1 and LOW==false==0 so your loop() can be simplified to one line:

void loop() 
{
  digitalWrite (LED,digitalRead (BUTTON));
}

This explains why you need the resistor

Be aware that since the new forum software was introduced the images have got messed up, I’ve not got round to sorting them out. I hope they still make sense.

Thanks for this, I had the words pull-down resistor in my mind, but not sure what it meant, I guess to pull it to ground.

So in reverse INPUT-PULLUP I assume means the pin has a voltage and a resistor so you can ground it safely.

I tried the INPUT_PULLUP and it worked a treat just had to swap the HIGH and LOW around, I also tried 1 & ) instead of HIGH & LOW which also worked, thanks for the tip. TBH I was just following the book, only on page 90 of the beginners book so a long way to go but already can see it’s potential.

I also tried the single loop line but the LED is illuminated until I press the button, and obviously the HIGH & LOW are no linger there to swap around, how would I fix this.

Thanks

const int LED=13;
const int BUTTON = 7;
int val =0;

void setup() {
pinMode (LED,OUTPUT);
pinMode(BUTTON, INPUT_PULLUP);

}

void loop() {

digitalWrite (LED,digitalRead (BUTTON));

}
void loop() {
digitalWrite (LED, !digitalRead (BUTTON));
}

The ‘!’ is the logical invert operator. It takes a true/false value and flips it. To convert an integer to true/false, anything non-zero is true and zero is false. So HIGH (1) becomes true and LOW (0) becomes false. Then the result on the flip is turned back into an integer: false becomes 0 (LOW) and true becomes 1 (HIGH).

1 Like

Spend a little time on the “Language Reference Page”.
Leo…

Or, you could connect the anode (long leg) of the LED to 5V and the cathode + series resistor to the output pin, the LED would be ON when the pin was LOW (connected to GND) and OFF when the pin was HIGH.

digitalWrite(LED,digitalRead(button));

Now you are getting the idea! :+1:

This is what I use; it creates a ‘kind of an alias’

#define ISPRESSED LOW
...
...
// if the button is pressed
if (digitalRead(yourPin) == ISPRESSED)
{
  // do something
}

And if the button is not pressed

#define ISPRESSED LOW
...
...
// if the button is not pressed
if (digitalRead(yourPin) != ISPRESSED)
{
  // do something
}

Thanks all for the answers, all very helpful replies, this is now solved and was down to a lack of knowledge in pull down and pull up resistors on the digital pins.

I guess what I take from this is that there are many ways to achieve what you want, and some are much less code intensive than others, the book seems to be doing it the very basic way (it is a basic book after all) but the way @johnwasser suggested dropped the code down to one line and removed the need of the physical drop down resistor to achieve the same outcome.

Thanks again, I will keep going and see how much of this sinks in.

1 Like