Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

How to Use digitalRead in Arduino? - Technetron Electronics

lucashenry

New Member
Hi everyone,

I'm new to Arduino and trying to understand how to use the digitalRead function in my project.

I have an Arduino Uno and a simple circuit set up with a pushbutton connected to pin 7.

I'm looking to read the button state, but I'm not sure about the correct way to use digitalRead.

Can anyone explain how to set up the pinMode, how to use digitalRead to check the button's state, and any tips for troubleshooting or common mistakes?

Any help or resources would be greatly appreciated!
 
Hi everyone,

I'm new to Arduino and trying to understand how to use the digitalRead function in my project.

I have an Arduino Uno and a simple circuit set up with a pushbutton connected to pin 7.

I'm looking to read the button state, but I'm not sure about the correct way to use digitalRead.

Can anyone explain how to set up the pinMode, how to use digitalRead to check the button's state, and any tips for troubleshooting or common mistakes in How to Use digitalRead in Arduino? - Technetron Electronics?

Any help or resources would be greatly appreciated!
thanks in advance for any help
 
Hi,

The Arduino IDE has an Examples section to show you exactly how to do that.

Also look around the Arduino site which has masses of info - below is the link to the Digital Read .
https://docs.arduino.cc/language-reference/en/functions/digital-io/digitalread/

Loads of info on the web for Arduino beginners including Ytube tutorials etc.

For testing out your switch you can use it to turn a led on or off or send suitable text to the Serial Monitor screen.

002422.jpg
 
Use pinmode to set pins as input or output, by name, eg.

pinMode(PA2, OUTPUT);
pinMode(PA3, INPUT);

digital read returns 0 for low or non-zero for high. You can either use variable = digitalread...
or simpler you can simply use the result directly in a conditional statement, like

if(digitalRead(PA3)) {
// pa3 was high if this executes
}

digitalwrite sets the pin high or low:

digitalWrite(PA2, HIGH);

You can also give names to pins at the start of the program so you can use a name rather than a pin directly, like

#define JS_L_PB PA6

then later

if(digitalRead( JS_L_PB )) {
// left joystick switch was pressed
...
 
When you read a mechanical switch it has "electrical bounce" where the contacts, for
a short amount of time, bounce and cause alternating 0'sd and 1's at the pin.

This is handled by debounce code or additional hardware :





Regards, Dana.
 

New Articles From Microcontroller Tips

Back
Top