Free Ebook cover Learning Robotics and Automation with Arduino from basic to advanced

Learning Robotics and Automation with Arduino from basic to advanced

4

(5)

78 pages

Working with Digital Inputs and Outputs on Arduino

Capítulo 7

Estimated reading time: 3 minutes

Audio Icon

Listen in audio

0:00 / 0:00

Working with Digital Inputs and Outputs on Arduino

Arduino is an open source platform that allows the creation of interactive electronic devices. One of the main features of Arduino is the ability to work with digital inputs and outputs. These features allow the Arduino to interact with a variety of electronic components such as LEDs, buttons, sensors, and more.

Understanding Digital Inputs and Outputs

The digital inputs and outputs are fundamental for the Arduino's interaction with the external environment. Digital inputs allow the Arduino to read values ​​from other devices, such as sensors or buttons. Digital outputs allow the Arduino to send values ​​to other devices such as LEDs or motors.

In programming terms, digital inputs and outputs are represented as binary values ​​- 0 or 1. A value of 0 generally indicates that a device is off or inactive, while a value of 1 indicates that a device is on or active .

Configuring Digital Inputs and Outputs on Arduino

To work with digital inputs and outputs on Arduino, you need to configure them in your code. This is done using the pinMode(), digitalWrite() and digitalRead() functions.

The pinMode() function is used to configure a pin as an input or output. For example, to configure pin 13 as an output, you would use the following code:

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

pinMode(13, OUTPUT);

To configure pin 12 as an input, you would use the following code:

pinMode(12, INPUT);

The digitalWrite() function is used to set the state of an output pin. For example, to turn on an LED connected to pin 13, you would use the following code:

digitalWrite(13, HIGH);

To turn off the LED, you would use the following code:

digitalWrite(13, LOW);

The digitalRead() function is used to read the state of an input pin. For example, to read the state of a button connected to pin 12, you would use the following code:

int buttonState = digitalRead(12);

If the button is pressed, the digitalRead() function will return HIGH. If the button is not pressed, it will return LOW.

Project Example: LED Control with Button

To illustrate how to work with digital inputs and outputs in Arduino, let's create a simple project that lights up an LED when a button is pressed.

First, connect an LED to pin 13 and a button to pin 12. Then write the following code:

int ledPin = 13;
int buttonPin = 12;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
}

void loop() {
  int buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }
}

This code reads the state of the button every cycle of loop(). If the button is pressed (HIGH), the LED is on. If the button is not pressed (LOW), the LED is off.

I hope this article has helped you understand how to work with digital inputs and outputs in Arduino. With this knowledge, you can start creating your own interactive projects!

Now answer the exercise about the content:

What do pinMode(), digitalWrite() and digitalRead() functions do in Arduino?

You are right! Congratulations, now go to the next page

You missed! Try again.

pinMode() is used to configure a pin as an input or output. digitalWrite() is used to set the state of an output pin. digitalRead() is used to read the state of an input pin. This is the correct sequence and functionality for these functions in Arduino programming, allowing interaction with external components.

Next chapter

Introduction to sensors and how to use them with Arduino

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.