digitalRead of input pin is not work properly

Hi,

As i mentioned the digitalRead on input pin is not work properly it seems to be randomly changes from LOW and HIGH and the input pin is connected to a voltage source 2v from small dc generator which driven by a dc motor.

if(digitalRead(8)==LOW){
    if(digitalRead(7)==LOW && digitalRead(11)==LOW){delay(3000);digitalWrite(3,HIGH);delay(1000);digitalWrite(2,HIGH);delay(3000);}
}
if(digitalRead(7)==HIGH){digitalWrite(2,LOW);digitalWrite(11,HIGH);}
if(digitalRead(8)==HIGH){digitalWrite(11,LOW);delay(10000);

In addition the pin 8 is input pin powered from usb and it's work good. but pin 7 is not i've changed the pin but still the same problem.

Thanks

If a pin is not connected to something, digitalRead() (or analogRead(), for that matter), or if it is connected to something which does not share a common ground or any other connections with the Arduino, then the pin is "floating" and will read random values due to ambient electrical noise.

2v is close to the threshold between what the board considers high and low, so if the voltage is unsteady, it could by right on the boundary between low and high, and appear to be transitioning randomly. If you want to examine a low voltage like that, why not use an analog pin to measure it?

Actually, i don't needs to measure the value but i am used it as a sensor to do another function as illustrated in the code.
I've put a 10k resistor between the pin and gnd but not solved!

Post a schematic and code.

From the ATmega datsheet a digital high is 0.6Vcc or more (3V or more when operating from 5VDC) and a digital low is 0.2Vcc or less (1V or less at 5V).

Anything between 1V and 3V is undefined.

But as DrAzzy says, you can read the analog value and set your own threshold in software. Since you are getting 2V out of the generator, maybe treating anything above 1V as high will work for you. (1V is an ADC reading of about 200 with the default 5V reference.)

As i mentioned the digitalRead on input pin is not work properly it seems to be randomly changes from LOW and HIGH and the input pin is connected to a voltage source 2v from small dc generator which driven by a dc motor.

"Actually I don't need to measure the value."

Actually, you DO , if you expect to troubleshoot the problem. How are WE supoosed to help YOU if you won't even MEASURE the voltage on the pin ?

You need to learn something about digital electronics, specifically about TTL LOGIC. Posting on the forum because you don't understand why a 2V signal is not being read correctly by an arduino digital input pin is putting the cart before the horse. You need to learn the basics of TTL digital logic BEFORE you try to use it. As stated. 2V is undefined. Digital logic is HIGHs and LOWs, specified in the TTL (or CMOS) standard. If you are trying to interface ANALOG signals to DIGITAL ELECTRONICS, you need to use COMPARATORs.. If you do not understand digital logic levels or Comparators, then you are not ready to do your project. Doing Electronics requires some knowledge of electronics. I would think that would be obvious. We don't expect you know much , but if you are asking about digital signals, it would seem logical that you would research what is a digital signal , really ? What is a high ? What is a Low ? Where does LOW leave off and where does HIGH start ? How else can expect to get anything to work ? Think about it for a second. You are asking us questions about digital signals. Do you think we should have to start the conversation with

"Excuse me sir , do you mind if I ask you if you know what a HIGH or a LOW is ?"

So, yeah, that was the long version. The short version is "The problem stems from unfamiliarity with digital electronics, starting with not knowing the definition of a LOW or a HIGH. Is it unkind for me to point this out ? That really depends on how on what our priority is here. Everyone that takes their time to try to answer posts is dedicated to the objective of answering the OP's question. In you case , that question is "Why doesn't my digital circuit work ?" I'm going to go out on a limb here and say:

"Because you don't know anything about digital electronics ?"

Your problem can be solved simply by implementing an op amp COMPARATOR . A comparator is a circuit that performs the function of translating a non digital signal (such as your 2V) into a high or a low. You set those thresholds with resistor voltage dividers. It is a bulletproof circuit and it will always work and you can set the levels to what ever you want. When it comes out of the comparator, it is a TTL class signal that will be understood by any digital circuit. One LT1215 has two op amps so you could make two comparators.
In a way, you could say a comparator is a simple A/D converter. The output is directly compatable with any arduino or any other circuit. All it requires is resistors. It can run off the arduino 5V, so you don't need another power supply. If the LT1215 is too exotic for you, you can rough it with a cheap Lm339 and some pull up resistors, and you can't tell the difference in performance.

You said you put a 10k resistor between the pin and GND. Was this BEFORE or AFTER you measured the voltage on the pin ?

Did you ever actually measure the voltage on the pin with a DMM ? How exactly did you conclude the pin was 2V ?

Why are using a DC generator as a DIGITAL input instead of ANALOG input ?

What is THAT supposed to accomplish ?

Is this for school ?

Would I be "off base" asking why you are using a uC if you don't know the definition of a digital signal ? Should you drive a car if you don't know what oil is ?

I am sorry for disturbing and my grateful for your method in talking with me… actually i’m at third stage of electrical and electronics engineering and nearly from graduation so please don’t compares your self with me , there is no wrong if i didn’t know a little.

In addition all you were mentioned to it i know it all!

Thanks to the other

and nearly from graduation so please don't compares your self with me , there is no wrong if i didn't know a little.

Can't be much of a school if you're in your 3rd year of Electrical Engineering and don't know what a digital signal is.

I've been in electronics for over 30 years and I had 12 years hands on electronics experience BEFORE I went to school to get my degree. I built about 30 digital circuit before I saw the inside of a Digital Electronics classroom. And just to be fair, you could have used Google to research it.

I am sorry for disturbing

I am not disturbed. I am simply pointing out the facts, starting with the fact that you should know what a digital signal is before you try to read one.

I'm not scolding you. I am simply pointing out that there is normal sequence that is expected with electronics. Microprocessors is considered more advanced that Digital Electronics, which is why we studied microprocessors in my 3rd year of the 3-year BS Electronics Engineering Technology curiculum at DeVry Institute. We studied Digital Electronics in the second year and analog in the FIRST year.
An arduino is a single board microcontroller containing digital and uP electronics (plus an ADC) so the correct sequence of learning is to learn the criteria of digital signals (the TTL specs) before uP interfacing. If I tell you I have a voltage of "x", and ask you if that is a valid TTL digital signal, your FIRST question SHOULD be:
"TTL or CMOS ?"
Your second question should be:
"Is it an INPUT or an OUTPUT ?"

Would you fly an airplane if you didn't know anything about flight controls ?

I know the 2v is undefined as digital signal but what made me wondering because the 2v work as digital signal in circuit but not in another one.. It was stupid question i know but it is not a huge wrong!
So i tried with analogRead it was recorded 400 unit resolution that opposites 2v and worked prefectly.
I know the default error in reading caused by connecting a wire withot voltage source. But i want to use this so how i could ?

DVDdoug:
From the ATmega datsheet a digital high is 0.6Vcc or more (3V or more when operating from 5VDC) and a digital low is 0.2Vcc or less (1V or less at 5V).

Anything between 1V and 3V is undefined.

But as DrAzzy says, you can read the analog value and set your own threshold in software. Since you are getting 2V out of the generator, maybe treating anything above 1V as high will work for you. (1V is an ADC reading of about 200 with the default 5V reference.)

Because of not accurate reading i’m made the condition like this:
if(x>=350 && x<=400) the readings was recorded in serial and this worked prefectly.

mi_ds:
Hi,

As i mentioned the digitalRead on input pin is not work properly it seems to be randomly changes from LOW and HIGH and the input pin is connected to a voltage source 2v from small dc generator which driven by a dc motor.

motors and generators put out spikes and noise - you need to smooth the output of your generator to
get a clean signal - 10uF or more perhaps.

However 2V isn't enough to reliably register as HIGH on a 5V Arduino anyway.

MarkT:
motors and generators put out spikes and noise - you need to smooth the output of your generator to
get a clean signal - 10uF or more perhaps.

However 2V isn't enough to reliably register as HIGH on a 5V Arduino anyway.

Sure it's not enough but i tried with analog and worked.
A switch connected between the 2v produced and the analog pin when the switch is closed it's ok, but while it's open that means the wire will still connected in analog pin and cause randomly reading! How i could solve this?

By complying with the request in reply #3.

I will.. But i was wondering if i could combine 2v dc offset with which produced from generator and this will be flexible controlled.
In case HIGH will be 4v (2v + 2v) and in LOW only 2v.

I don’t know why i’m banned from the forum! Now, I’m using a web proxy to say i succeded in solve the problem of randomly reading in case which the wire still connected to the analog without any voltage source…
Added a toggle switch with two line the COM is to analog and L1 to the dc generator and L2 to the ground (I’ve used toggle switch because i need it in my circuit). You can use instead of it an npn transistor this will work.

The circuit in attachments!

That circuit cannot work, no ground wire to Arduino, transistor backward, ...

int rpmPin = A0;
word x;
word avg;

void setup() {
}

void loop() {
  avg = ((avg + analogRead (rpmPin) & 0xFFF) / 4) + 100;
  x = analogRead (rpmPin);

  if (x >= (avg - 50)) {
    // input is HIGH
  } else {
    // input is LOW
  }
}

outsider:
That circuit cannot work, no ground wire to Arduino, transistor backward, ...

Thanks for this because i forgot the GND ..

updated it.

dlloyd:

int rpmPin = A0;

word x;
word avg;

void setup() {
}

void loop() {
  avg = ((avg + analogRead (rpmPin) & 0xFFF) / 4) + 100;
  x = analogRead (rpmPin);

if (x >= (avg - 50)) {
    // input is HIGH
  } else {
    // input is LOW
  }
}

Can you explain more about the code. Thanks

Please ignore the previous test code. If the offset stays the same (2V), then you could try this to test the generator input, output is on LED:

int generatorPin = A5;
int ledPin = 13;
int generatorReading;

void setup() {
  pinMode(13, OUTPUT);
}

void loop() {
  generatorReading = analogRead (generatorPin);

  // generator voltage varies from 2-4V, using 1V hysteresis
  if (generatorReading > 716) {  // > 3.5V
    digitalWrite(ledPin, HIGH);
  } else {
    if (generatorReading < 512) {  // < 2.5V
      digitalWrite(ledPin, LOW);
    }
  }
}