Go Down

Topic: No reading when doing digitalRead() (Read 549 times) previous topic - next topic

jscoulter

Hi Guys.
I have a strange issue that I am not able to solve.
I have a circuit that has a switch connected to pin D8 as INPUT and have an LED connected to pin D7 for OUTPUT.
I have the switch connected as per this circuit: http://webzone.k3.mah.se/projects/arduino-workshop/projects/arduino_meets_processing/instructions/img/pic_switch.jpg

The resistor I am using is a 10K resistor with the circuit running of 5V.

my code in the loop() procedure is:

iState =  digitalRead(buttonPin);
if (iState)
    {
      digitalWrite(yellowled, HIGH);
    }
  if (!iState)
  {
    digitalWrite(yellowled, LOW); 
    }

The problem I have is, when I turn the switch off, it acts as expects, the LED goes off ...thats all good, BUT when I turn the switch back on again, the digitalRead() does not seem to work !
I have used digitalWrite(yellowled, iState); straight after the digitalRead() and is STILL doesnt work....that is the LED doesnt light up.
I have checked and I do get power hitting D8 when the switch is on, and no power when turned off, so that part of the circuit is working. It just doesnt seem to function when the digitalRead() is performed.

2 Ideas I had were: 1. the program is trying to read the input but it cant and get stuck and cant move on, 2. the resistor might need to be more like 1K instead of 10.

I hope I have provided enough information for someone to be able to help.

Thanks, Jeremy

fungus

Maybe we can help if you post the complete code...

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

majenko

Yes, we need to see if you are initialising the ports correctly, amongst other things.

Also, please post a full schematic of your setup - not someone else's schematic, but trace your connections as they actually are on your setup and draw them on paper for us.  That not only helps us, but it also helps you.

You can also massively simplify your code snippet:
Code: [Select]

if (digitalRead(buttonPin))
{
  digitalWrite(yellowled, HIGH);
} else {
  digitalWrite(yellowled, LOW);
}


Or even more:

Code: [Select]

digitalWrite(yellowled,digitalRead(buttonPin));

jscoulter

yes I COULD simplfy my code....but thats not the problem. I doubt that its related to the issue because I have eliminated a couple of things because I have already done:
            digitalWrite(yellowled,digitalRead(buttonPin));
and it made not difference which is why I decided to come to the forum.

There are 2 reasons why I posted the image in the original message that showed my schematic

1. thats how I am doing it !
2. at 11:30pm I didnt really want to spent another 1/2hr doing a schematic to replicate the image which as I have said, is exactly how I am doing it.

Here are my variable declarations and my setting up of the pin modes

const int buttonPin = 8;
const int ScrenPowerPin = 7;
const int yellowled = 10;

void setup() { 
   pinMode(yellowled, OUTPUT);
   pinMode(buttonPin, INPUT);
   pinMode(ScrenPowerPin, OUTPUT);

//there is other code but its not relevant for this issue
}

AWOL

Quote
there is other code but its not relevant for this issue
Says who?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

majenko


There are 2 reasons why I posted the image in the original message that showed my schematic

1. thats how I am doing it !
2. at 11:30pm I didnt really want to spent another 1/2hr doing a schematic to replicate the image which as I have said, is exactly how I am doing it.

If that is how you are doing it, then it should be working.

Ergo, that is not how you are doing it.

The best way of checking it is to do a schematic.

fungus


//there is other code but its not relevant for this issue


From what you've posted so far it looks like you're trying to power a "Scren" using an output pin. Maybe that's a bad idea and it's what's causing your problem.

But hey, all that's irrelevant... along with your schematic, a list of what's connected to the Arduino, how you're powering it.

None of that could possibly cause a problem though, it has to be digitalRead() that's failing, right?
No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

jscoulter

yes I am powering a screen...although it only needs 3v to power it, so I think it will be ok.
However....I am checking the voltage on PIN D7 WITHOUT the screen plugged in. So its not that :-)

I will revisit my circuit tonight when I get home....its work time for me now :-(  and just dbl check it etc.

Thanks for the help/pointers/ideas thus far.

Jeremy

AWOL

Quote
although it only needs 3v to power it

At what current?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

jscoulter

have to check when I get home.

Of course.....it could be I am going about this the wrong way :-)
What I have done is built a wee project that connects a GPS to my circuit, and I have a little 1.8'' screen that shows the releivant info I want to disply.
I have ti set up so I can switch between battery backup and a 5v power source. The easy thing to do is have the screen powered off the 5v input source, BUT I also want the screen to run on the battery backup if needed. So I thought, add a switch, have the arduino pickup that I have turned the switch off, which in turn switched the screen off (which works now), then when I turn it back on the screen should power up again...this doesnt work. There is also a wee complication where I need to re-init the screen after its powered on which is why I am taking the course I am on now.

Jeremy

MarkT


yes I am powering a screen...although it only needs 3v to power it, so I think it will be ok.
However....I am checking the voltage on PIN D7 WITHOUT the screen plugged in. So its not that :-)

I will revisit my circuit tonight when I get home....its work time for me now :-(  and just dbl check it etc.

Thanks for the help/pointers/ideas thus far.

Jeremy


3V?  And what is your supply voltage?  And what voltage was D7 when the screen was plugged in?

In the absence of a circuit I'll just have to guess that you are directly powering some sort of "screen" directly from pin D7.  Ouch!

You will likely be overloading the D7 pin, potentially damaging its transistors or the whole 328 chip.  Output pins are only able to take absolute maximum of 40mA.  These are logic signals which are just able to power a single LED, but must be boosted with some form of transistor/MOSFET/relay to handle larger currents or to switch power to anything with decoupling capacitors.

[ We ask for complete code and complete schematic because often the real problem lies somewhere other than the
poster supposes (otherwise they would have figured it out already!!)  There is no easy or reliable way to test someone else's code
without having their sketch in full for instance.  Many times the problem is thought to be code when its circuit-related, or vice-versa ]
[ I won't respond to messages, use the forum please ]

jscoulter

Ok. I didnt get time to check. Maybe I should just use a small relay, or, maybe is just tuff...if its on battery, it just stops power to the screen...might just do that I think. Oh well :-)

Go Up