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:

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.

Article image Introduction to sensors and how to use them with Arduino

Next page of the Free Ebook:

8Introduction to sensors and how to use them with Arduino

3 minutes

Obtenez votre certificat pour ce cours gratuitement ! en téléchargeant lapplication Cursa et en lisant lebook qui sy trouve. Disponible sur Google Play ou App Store !

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text