Hello everyone,
I am beginner to using the Arduino, I use Arduino Uno.
We have one project in which I need to read a voltage of 5V from a sensor that is short about 3ms. When the sensor does not read anything I have 0V and when it reads I have 5V voltage. I need to read that voltage on some pin as a signal and to output 5V as a signal on the other pin and extend it to 50ms.
I need the output signal to be 5V when the sensor gives 5V, and to be 0 when the sensor reads nothing.
3ms is short for a human, but very slow for an Arduino.
Just digitalRead the input pin, and write a HIGH to an output pin that you set LOW again after a 50ms delay.
You're not storing the outcome of your digitalRead anywhere. Apart from the problem pointed out by @jremington, you seem to compare the pin value (int sensor = 13) with 'HIGH', which doesn't make sense.
Try something like
if (digitalRead(sensor))
{
//Rest of code here
}
Another issue is the use of delay() to keep your output high for 50ms. During the delay period, you will miss any 3 ms signals that you might have wanted to capture. Try something creative with millis() (many tutorials on this I understand, but it's not rocket science anyway) and/or Google on 'non-blocking code Arduino' etc.
For the test I used 5V from the Arduino, when I bring 5V to pin 7 the LED "L" on the Arduino starts to light up. When I remove the voltage of 5V LED "L" stops lighting. This confirms that the Arduino is reading the signal. But even if I touch PIN 7 very quickly, the LED "L" remains lit. It lights up for about seven seconds and then goes out. I also read a long signal on the oscilloscope. It seems to me that the Arduino is reacting slowly to change.
Do any of you perhaps know why the Arduino behaves like this?
I tried something with another Arduono Uno device and it behaves the same.
Try to avoid using pin13 as input or you will have issues with small boards like a Nano.
Basic sketch attached. If you want it to do more than that, then the delay() should be replaced with millis() timing.
Leo..
const byte sensorPin = 2;
const byte outPin = 13; // also flashes the built-in LED
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
if (digitalRead(sensorPin)) {
digitalWrite(outPin, HIGH);
delay(50);
digitalWrite(outPin, LOW);
}
}
Hard to say without a schematic. In principle the response of the arduino should be a few microseconds with the code you posted. A possible cause is that you have some amount of capacitance at pin 7 that charges as soon as you touch it with 5V and that discharges slowly through the leakage current of pin 7.
An input pin is 'floating' when nothing is connected to it, and can take any digital value in this state.
You must have a 'bleed' resistor from input pin to ground. Any high value would do (100k-1Meg).
Where does that '5volt' come from.
It's ok if it comes from the Arduino itself, but it could be wise to connect it via a (10k) protection resistor if it comes from a device not powered from the Arduino.
Leo..
The problem arose when I used 5V directly from the Arduino itself to pin 7 for the test. For some reason, it did not want to break the voltage quickly, even if I just touched pin 7.
When I connected the Arduino to the circuit I made to reduce the 12V from the photo cell to 5V to use as an input signal. Then Arduino started working properly.