Last Updated: 11/08/2021

Functions Lesson 2 - digitalRead()

Functions >> Functions Lesson 2 - digitalRead()

Lesson Contents

Reading a digital pin with digitalRead()
Problems with digital pin floating values
External Pull Down resistors
Arduino Pull UP resistors

Example 1: digitalReadv1.ino

Click to Download code:digitalReadv1.ino

A basic sketch that shows how to set up and read from a digital pin using digitalRead().
The sketch also shows up the issue of a poorly designed circuit.

test the sketch first with no circuit attached to your Arduino, as the circuit is running watch the serial monitor and you will see the value changes from 0 to 1 and back. Touching the Arduino with your hand will also causes this to happen.

 
/* 11/08/2021
 *  digitalReadv1
 *  
 *  Purpose: to read an input on a digital pin
 * 
 *  pin 8 used for incoming signal
 * 
 */
 //constant integer for pin 8, the pin we will read from.
const int readPin = 8;

void setup() {
  Serial.begin(9600);
  Serial.println("digitalReadv1");
  Serial.println(" ");
  //pinMode to INPUT means that the Arduino is expecting to read an incoming value from this pin
  pinMode(readPin,INPUT);
}

void loop() {
  //Local variable declared
  int readValue;
  //Read the value on the pin (1 is HIGH 5v, 0 = LOW 0v);
  readValue = digitalRead(readPin);
  Serial.println(readValue);
  //Wait for 0.1 seconds
  delay(100);
}

The circuit below adds a Pull Down resistor (in this example 330ohm), the usual value for this resistor is 1k - 10k with around 4.7K ohm being the average. This resistor forces the pin to remain LOW (0v) until the switch is pressed stopping the value from changing even if you touch the board as in the first experiment.

Pull Up resistor circuit dagram

Example 2: digitalReadv2.ino

In this example we will use digitalRead() with a built in Pull Up resistor.

Connect up your circuit as below depending on if you are using the sensor shield or not.

If you do not have a switch just touch the two wires together.

Arduino built in Pull Down resistor circuit
Click to Download code:digitalReadv2.ino

Notice the difference in the pinMode(), in this example it is set to pinMode(readPin,INPUT_PULLUP)
This tells the Arduino to add it's own internal Pull Up resistor.
Notice that a Pull UP resistor has a default value (switch open) or 1 (HIGH). With a Pull Down resistor the default value is 0 (LOW).

 
/* 11/08/2021
 *  digitalReadv2
 *  
 *  Purpose: to read an input of a digital pin
 *  Using a pull up resistor
 * 
 *  Pin 8 used for incoming signal
 */
 //Constant integer delared for the pin we will read
const int readPin = 8;

void setup() {
  Serial.begin(9600);
  Serial.println("digitalReadv2");
  Serial.println(" ");
  //pinMode to INPUT_PULLUP to read the incoming signal
  //This also tells the Arduino to use one of it's internal Pull UP resistors
  //on the pin to save us adding our own external version.
  pinMode(readPin,INPUT_PULLUP);
}

void loop() {
  //Local variable declared
  int readValue;
  //Read the value on the pin (1 is HIGH 5v, 0 = LOW 0v);
  readValue = digitalRead(readPin);
  Serial.println(readValue);
  //Wait for 0.1 seconds
  delay(100);
}

Additional Resource Links

Don't forget to use the Reference in you Arduino IDE for more help on this lesson. see pinMode() and digitalRead();

Comments


This site has been designed to be child friendly, this means that comments cannot be added to videos or directly to the site.
To add a comment or ask a question please email the address in this image: and use Functions Lesson 2 - digitalRead() as a reference.