Go Down

Topic: Reading the value of an output pin? (Read 649 times) previous topic - next topic


Is it possible for the arduino to digitalRead the value (high or low) of it's own pin that is supplying 5v to the base of a relay driver circuit without using another (input) pin connected to that output pin?
I am using a Duemilanove (I have an UNO as well)

here is an example:

Code: [Select]
  if (lastSw == LOW && currentSw == HIGH && outPin == LOW);
    digitalWrite(outPin, HIGH);

lastSw and currentSw are inputs but outPin controls the relay circuit. can i read outPin like this or is it illogical?
as you can see, i want to turn outPin on if it is not already on and the other two inputs are where they need to be.

any help is greatly appreciated!


I would think the correct way to do that is in software. Declare a variable before you setup function (global variable) like boolean running = false;

Then any time you perform a:

digitalWrite(outPin, HIGH);  // turn motor on
follow it immediately with a
running = true;  // capture that motor is on

and any time you perform a:

digitalWrite(outPin, LOW); // turn motor off
follow it immediately with a
running = false; // capture that motor is off

Now your sketch has a variable called running that you can test any time or where in your sketch to see the present state of the output pin that is controlling your motor.



If lastSw is low and current Sw is high, would outPin stay high even if it was high already?

Because if so, there's no point testing it- just send outPin high based on the other two pins, regardless of outPin's existing state.

Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0


Jan 14, 2013, 06:57 pm Last Edit: Jan 14, 2013, 07:22 pm by PaulS Reason: 1
Code: [Select]
if (lastSw == LOW && currentSw == HIGH && outPin == LOW);
If the condition evaluates to true, do nothing (;). Otherwise, do nothing.

Why bother with the evaluation?
The art of getting good answers lies in asking good questions.


here is an example:

Code: [Select]
 if (lastSw == LOW && currentSw == HIGH && outPin == LOW);
   digitalWrite(outPin, HIGH);

To make any sense, that would need to be digitalRead(outPin) == LOW.

On my UNO, with IDE 1.0.1, a digitalRead() to an OUTPUT pin returns the last value that was output. I would expect this behaviour to be consistent with other cores and versions. So, what you're trying to do should work, as long as you do actually test the state of the pin rather than the pin number and lose that bogus semicolon.

I only provide help via the forum - please do not contact me for private consultancy.


Here's the basic logic that I use to handle relays.

Code: [Select]

byte relayPin;                             // point to the relay's pin
int relayState = LOW;                  // keeps the relay state as HIGH or LOW

void setup(){
    pinMode(relayPin, OUTPUT);            // set the pin to output
    digitalWrite(relayPin, relayState);    // write the current state to it

// now we'll check the relay to see if it should be on or off
void loop(){
    int flag = LOW;                   // start with a flag set to low
    if (The relay should be on for whatever reason)   // obviously not real code
         flag = HIGH;
    // now compare the flag to the relayState
    if (flag != relayState)
         relayState = flag;                                  // assign flag's value to relayState
         digitalWrite(relayPin, relayState);         // write this new state to the pin

It's pretty sane and easily allows for more than one reason to keep the relay on. The relay only gets changed if needed.


Isn't the state of all pins available in the MCU registers?

Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts


Jan 15, 2013, 12:55 am Last Edit: Jan 15, 2013, 01:03 am by NFrank89 Reason: 1
First off, wow! i dont think i've ever used a forum with so many useful answers after a single question! thanks guys!

so i guess to put this in perspective, ill fill you in a bit more. this is my first *almost* completely original sketch. it's function is to emulate a smart key system like toyota uses. so if the rf key is detected and the brake is held while the button is pushed the car starts right up. if the brake is not held and the button is pushed it goes to accessory, push again, ignition. once at ignition you can hold the brake and push again to start the vehicle or you can just push again and the car turns off. right now im not worried about the rf key since this vehicle is still in development and i can code that in a bit further down the line.

the safety feature is making sure the NSS (neutral safety switch from the transmission that shows ground in park or neutral) is grounded so the vehicle cannot start in gear.

the lastSw and currentSw are variables from a debounce code i borrowed from another sketch. honestly, i dont know if i'm even using it correctly.

my assumption is that by making sure the last switch state and current switch state does not match in each possible situation it will prevent crazy things from happening if the user holds the button down for too long since it is always checking to make sure the last button state was not high (pressed) before running another line of code.

here is my sketch if you care to look it over. if you see anywhere i can make it more efficient please let me know! i appreciate your time and helpfulness!

Code: [Select]
  Push button Ignition Switch/Starter (Like Toyota SmartKey)

Created by Nick Franklin - Franklin Automotive Electronics
January 14, 2013

//set pin numbers:

const int brakePin = 7;   // brake petal +input
const int engRun = 6;     // from alternator running +input
const int swPin = 5;      // ignition push button closed = HIGH
const int stPin = 4;      // +output to starter relay
const int igPin = 3;      // +output to ignition relay
const int led = 13;       // i synchronize this led with other outputs for diagnostic purposes
const int nssPin = 8;     // -input from transmission in park or neutral so you can not start in gear
boolean lastSw = LOW;
boolean currentSw = LOW;

void setup()
  pinMode(stPin, OUTPUT);
  pinMode(igPin, OUTPUT);
  pinMode(led, OUTPUT);   
  pinMode(swPin, INPUT);
  pinMode(engRun, INPUT);
  pinMode(brakePin, INPUT);
  pinMode(nssPin, INPUT);


// Debounce code to prevent button misread
boolean debounce(boolean last)
  boolean current = digitalRead(swPin);
  if (last != current);
    current = digitalRead(swPin);
  return current;

void loop()
  currentSw = debounce(lastSw);
  //if the ignition is off but the brake is held and the ignition button is pushed
  //program will turn the ignition on, wait 3sec for fuel pump to prime, then activate the starter relay.
  if (lastSw == LOW && currentSw == HIGH && igPin == LOW && nssPin == LOW && brakePin == HIGH);
    digitalWrite(igPin, HIGH);
    digitalWrite(stPin, HIGH);
    //once the engine running signal is recieved from the alternator the starter relay is deactivated.
    if (engRun == HIGH);
    digitalWrite(stPin, LOW);
  // if the ignition is off and the button is pressed once ignition will turn on
  if (lastSw == LOW && currentSw == HIGH && igPin == LOW);
    digitalWrite(igPin, HIGH);
  // if ignition is on, brake is pressed, engine is not running, and the button is pushed
  // then activate the starter relay.
    if (lastSw == LOW && currentSw == HIGH && igPin == HIGH && nssPin == LOW && brakePin == HIGH && engRun == LOW);
    digitalWrite(stPin, HIGH);
    //once the engine running signal is recieved from the alternator the starter relay is deactivated.
    if (engRun, HIGH);
    digitalWrite(stPin, LOW);
  // if the engine is running and the button is pushed the ignition relay is deactivated and the vehicle is turned off
    if (lastSw == LOW && currentSw == HIGH && igPin == HIGH && engRun == HIGH);
    digitalWrite(igPin, LOW);

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131