Serial Printing Voltage in a simple circuit

Hey guys,

I have built a circuit intending to print the voltage being read in an analogue pin (A0). The serial monitoring should be triggered when I press a button, triggering an input of “HIGH” into a digital pin (D2).

EDIT: [forgot to actually describe my problem]
For some reason unknown to me, my serial output is jaggered and all over the place. It is also triggered randomly. I’m not sure where the problem lies.

Thanks in advance friends :slight_smile:

My circuit diagram is attached.

unsigned long time;

void setup() {
  pinMode(2, INPUT);
  Serial.begin(9600);
}



void loop() {
  
 if(digitalRead(2) == HIGH)
 {
    Serial.print("Time:  ");
    Serial.println("Voltage: ");
    time = millis();
    Serial.print(time);
    Serial.println("  ");
    Serial.println(analogRead(A0));
 }
}

What's the question?

Why have you connected the switch to pin 3 but you're reading pin 2?

Sorry my bad, I changed it to pin 3 to see if that was the problem. I'm not getting the serial output I'm trying to obtain and not sure why. It's all jittery and random.

Please help <3

If the action is triggered randomly, your switch is wired incorrectly. Using the internal pullup resistor is much easier than adding an external resistor.

Your order of print() and println() statements is bizarre.

Post a picture of your actual wiring.

My wiring is as attached.

This is the first project in which I’m using Serial.Print etc so I’m not too sure about which methods are efficient and which ones are bizarre. Thank you for the help though, always looking to learn more.

It is the order of print() calls that is bizarre. I'd print() "Time: " and then println() the time. Then, I'd print() "Voltage: ", followed by println() of the voltage.

I can't tell anything from your pictures - too dark.

The way I'd like to have the printed final result is like a table:

Time: Voltage:
xxxxxx xxxx

Hence the strange order of print commands.

I'll try and get better pictures. But can you confirm that the code and the commands together should work?
Cheers

Quick note: you are printing ADC (analog to digital converter) output, NOT voltage. If you want to have a value between 0-5V instead of a value between 0-1023, you will need to convert the return value of the analogread.

I also think your schematic for the pushbutton is wrong, not to mention, you need to denounce the button in software.

Yep, the conversion from 0 - 1023 to 0 - 5V is the next challenge. How do I go about denouncing the button in the software?

Also, I did a troubleshoot and everything works fine except for the IF loop. The LED in my circuit works fine when the button is pressed indicating that there is current through the LED. So D2 should be receiving a Digital "HIGH". Which is why I'm confused why it isn't giving me anything in the Serial Monitor when I place the IF loop back again.

How do I go about denouncing the button in the software?

Read Power_Broker's post again. Nothing was said about denouncing the button.

To print the data the way you show,

   Serial.println("Time:   Voltage:");
   Serial.print(time);
   Serial.print("   ");
   Serial.println(voltage");

The word you want is debouncing not denouncing.

Read the switch every ~50ms look for a change in state.
Do what needs to be done if that change went from HIGH to LOW or vice versa.

.