# How to read and use the voltage of 5V from the sensor as a signal and extend that signal and output it to some pin

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.

Can someone help me?

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.

Let's see some code.
Leo..

Hi Leo,

This is code I made, but it doesn't work.

int sensor = 13;
int PHD = 7;

void setup() {
pinMode(PHD, OUTPUT);
pinMode(sensor, INPUT);
}

void loop() {
if (sensor=HIGH);
{
digitalWrite(7, HIGH);
delay(50);
digitalWrite(7, LOW);
}
}

``````if (sensor=HIGH);
``````

"=" is the assignment operator. "==" is used for comparisons.,

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.

Hello guys,

In the meantime, I changed the code.

int sensorState = 0;

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

void loop()
{
if (sensorState == HIGH) {
digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}
}

This should work.

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() {
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..

Hi guys,

I did it.

This code works:

int sensorState = 0;

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

void loop()
{
if (sensorState == HIGH) {
digitalWrite(13, HIGH);
delay(50);
} else {
digitalWrite(13, LOW);
}
}

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.

Thank you so much for help!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.