So, I have a test code that uses digital pin 5 as output and digital pin 7 as input. So i have connected both of them together and also hooked up the serial monitor for the digital pin 7. I have also configured it so that the D5 pulses.
The serial monitor only prints 0 instead of pulsing value. I have tried using analog pin as input but it also prints 0. I suspect i have a problem with digital pin output. (I'm really newbie at this arduino stuff)
int pulsePin = 5; // digital pin 5 as output
int inputPin = 7; // digital pin 7 as input
void setup() {
pinMode(pulsePin, OUTPUT); // set pulsePin as output
pinMode(inputPin, INPUT); // set inputPin as input
Serial.begin(9600); // initialize serial communication
}
void loop() {
digitalWrite(pulsePin, HIGH); // set pulsePin HIGH
delay(500); // wait for 500ms
digitalWrite(pulsePin, LOW); // set pulsePin LOW
delay(500); // wait for 500ms
int inputVal = digitalRead(inputPin); // read inputPin value
Serial.println(inputVal); // print inputPin value to serial monitor
delay(10); // small delay to avoid printing too fast
}
Here is the above code. Please help me. I dont wanna buy another arduino nano just for this stuff...
I have moved your topic to an appropriate forum category @rnnscientist420.
In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.
This is an essential part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. It contains a lot of other useful information. Please read it.
I expected it to output pulse value is because the D5 pin (this pin outputs a pulse)
is connected to D7 which will also read a pulse value thus printing a pulse value to the serial monitor. It would be really helpful if you give me the corrected part of the code.
To get changing values you need to read the input in both states:
void loop() {
digitalWrite(pulsePin, HIGH); // set pulsePin HIGH
delay(500); // wait for 500ms
int inputVal = digitalRead(inputPin); // read inputPin value
Serial.println(inputVal); // print inputPin value to serial monitor
digitalWrite(pulsePin, LOW); // set pulsePin LOW
delay(500); // wait for 500ms
inputVal = digitalRead(inputPin); // read inputPin value
Serial.println(inputVal); // print inputPin value to serial monitor
}
This code isn't printing the value of D7 to serial monitor is it? Or is it just outputting the state of the D5.
I know this is a dumb simple shit. Pls don't judge.
D7 pin do not read anything itself. It do it ONLY once in period - in exact moment as you call the digitalRead() function. As the pin state at this moment is LOW - you always see 0 in monitor.
That was the problem. I was trying to troubleshoot my Arduino pins as the serial monitor isn't providing proper values. I'm using the pin D5 as a pulse so that I can check if my other pins are working correctly. But turns out they aren't working correctly or there is something wrong with the code I wrote since the value of the input pins printing to the serial monitor isn't consistent with the pulse of the D5.
You don't seem to be listening to what @b707 is telling you.
There is no problem with your code, your Nano, Serial Monitor or Tinkercad. All are performing exactly as expected. They are not performing in a confusing way, they are performing in a completely predictable way.
You gave yourself a username containing "scientist", so let me make an analogy that may help you.
Imagine a dripping tap, a column of falling water drops. You shine a stroboscope on it and adjust the speed of the stroboscope so that the drops appear to hang stationary in mid-air. Is it really the same drops you see each time the stroboscope flashes? Are the drops really hanging in mid-air? No. Each time the stroboscope flashes, your eyes get a brief sample of the stream of drops.
Your code is doing an analogous thing. The output pin is pulsing. But you sample the pulses (read the input pin) at the same point in each cycle, so you see the same result each time.
Another way of doing it would be to use the tone() function in setup to generate your pulses, then use loop to only do the reading/printing.
int pulsePin = 5; // digital pin 5 as output
int inputPin = 7; // digital pin 7 as input
void setup() {
pinMode(pulsePin, OUTPUT); // set pulsePin as output
pinMode(inputPin, INPUT); // set inputPin as input
Serial.begin(9600); // initialize serial communication
tone(pulsePin, 40); // output 40Hz signal on pulsePin
}
void loop() {
int inputVal = digitalRead(inputPin); // read inputPin value
Serial.println(inputVal); // print inputPin value to serial monitor
}