Getting Started with Arduino Input/Output: A Beginner’s Guide

This article introduces the implementation and demonstration of basic input/output with Arduino, detailing how to use switches for digital input and potentiometers for analog input. The article provides a list of required materials and detailed circuit connection steps, while explaining the working principles of the code and key functions.

Arduino is one of the widely used development solutions for both beginners and professionals. By mastering basic inputs and outputs, you can construct more functionalities, perform more operations, and achieve more diverse designs. Programming with Arduino can spark innovative thinking, allowing you to complete more complex projects step by step and explore infinite possibilities. The following video will introduce how to use switches for digital input and variable resistors for analog input. Hurry up and click to watch!

Once you have grasped the basics of Arduino and can control an LED with code, it’s time to learn about Arduino inputs, which are the electrical signals used to provide information to the Arduino development board. The Arduino evaluation board can be programmed to receive electrical signals and take corresponding actions based on these inputs.

Getting Started with Arduino Input/Output: A Beginner's Guide

A switch is a mechanical device that typically uses a lever or button to connect or disconnect a circuit. To build this circuit, you will need a small push button, which is one of many different types of switches.
It is recommended to use an Arduino Uno (1050-1024-ND) or a compatible development board such as Adafruit Metro (1528-1214-ND), Sparkfun Redboard (1568-1977-ND), or Seeeduino (102010026-ND) to get started.

Have questions after watching the video? Or do you have experience using Arduino that you would like to share? Copy the link: https://www.digikey.cn/zh/forum/t/topic/4129

To learn how to build the circuit featured in this issue, please prepare the following materials:

  • Arduino Uno or a compatible development board, along with a USB cable
  • Non-soldering breadboard
  • Jumper wires or single-core wire
  • 3V through-hole LED
  • Through-hole resistors, 100-1000 ohms and 10K ohms
  • Touch switch
  • Potentiometer
Getting Started with Arduino Input/Output: A Beginner's Guide
Start with the basic setup for prototyping a new circuit: first, assemble the Arduino UNO evaluation board and the non-soldering breadboard on a fixed evaluation board, connect the 5V and ground lines from the Arduino evaluation board to the rails on the breadboard, and then connect to the rails on the other side of the breadboard. This is the basic setup for prototyping a new circuit.
Getting Started with Arduino Input/Output: A Beginner's Guide
Next, connect an output LED, connecting the longer positive lead to pin 13 and the negative lead through a small value resistor (100 ohms – 1K ohms) to ground.
Getting Started with Arduino Input/Output: A Beginner's Guide
Next, add the button to the breadboard; the button is typically designed to span across the center divider of the breadboard. If the button does not fit snugly, you may need to slightly bend its pins.
Getting Started with Arduino Input/Output: A Beginner's Guide
One side of the button connects to pin 2, while the other side connects to ground through a larger value resistor (10K ohms).
Getting Started with Arduino Input/Output: A Beginner's Guide
Here, diagonal pins are used instead of other connections because this is a reliable way to ensure correct connections regardless of how the switch is internally wired. You can also use a multimeter to check, probing the two pins on the switch and pressing it to see which are connected.
Getting Started with Arduino Input/Output: A Beginner's Guide
Once the circuit is connected, you can plug in the USB cable and upload the new program.
Getting Started with Arduino Input/Output: A Beginner's Guide
Open the downloadable Arduino software, click File > Example > 02.Digital > Button.
After uploading the code to the Arduino evaluation board, when the button is pressed, the LED will turn off from being on.
Getting Started with Arduino Input/Output: A Beginner's Guide
The beginning of this program contains some constants, which are similar to variables used to store information. However, as you know, constants do not change throughout the program, making them very suitable for storing pin numbers. They occupy less memory space than variables.
Getting Started with Arduino Input/Output: A Beginner's Guide
Getting Started with Arduino Input/Output: A Beginner's Guide
Line 36 configures pin 2 as an input mode, allowing it to receive the button’s state
Getting Started with Arduino Input/Output: A Beginner's Guide
In the main loop, the digitalRead() function checks the state of pin 2, which can be 5 volts (i.e., HIGH) or ground (i.e., LOW), and stores this state in a variable called buttonState
Getting Started with Arduino Input/Output: A Beginner's Guide
Line 44 contains an if() statement that uses comparison operators to perform conditional checks, such as greater than, less than, or in this case, equal to, indicated by two equal signs. If the condition is met, the code inside the braces will be executed, turning the LED on (HIGH). If the condition is not met, the code inside the else statement will be executed, turning the LED off (LOW).
Getting Started with Arduino Input/Output: A Beginner's Guide
In a resting state, there is no connection between the switch’s pins. Pin 2 is connected to 5 volts through a larger value resistor (10K ohms). When the button is pressed, the switch leads are connected, and pin 2 is grounded without any resistor. Since the current will take the path of least resistance, the pin will strongly sense the connection to ground and ignore the weak connection to 5 volts through the 10K ohm resistor. However, when there are no other signals, such as when the switch is not pressed, the pin can only sense the weak connection to the 5 volts.
Thus, this resistor pulls the pin up to 5 volts, hence it is called a pull-up resistor. If there is no pull-up resistor connected, pin 2 will not be connected to anything before the button is pressed, which is known as a floating pin and can produce random noise from static electricity and electromagnetic interference.
Getting Started with Arduino Input/Output: A Beginner's Guide
Similarly, resistors can also be used to connect a pin to ground, which is called a pull-down resistor. You can then connect the other side of the button to the power supply instead of ground.
Therefore, to change the function of the button, you can either change the wiring of the circuit or modify the code, but never do both at the same time! In this case, the latter is simpler, but this is not always the case.
Getting Started with Arduino Input/Output: A Beginner's Guide
Next, edit the if() statement and its comments on lines 43 and 44, changing HIGH to LOW, then upload the updated code to the development board to check whether the button can now turn on the LED instead of turning it off.
Getting Started with Arduino Input/Output: A Beginner's Guide
Many of Arduino’s pins have built-in pull-up resistors, and the tiny resistors within the chip are designed for this purpose. In the setup, you can enable the built-in pull-up resistors, eliminating the need for additional resistors on the breadboard.
You can also use the serial monitor, which is a way to check different information in the code by reporting to the computer via USB. To understand how it works, go to File > Examples > 01 Basics > DigitalReadSerial in the Arduino software, then upload the example program to your Arduino.
Getting Started with Arduino Input/Output: A Beginner's Guide
To establish a serial connection, you need to use the Serial.begin() function in the setup, with 9600 as the baud rate, indicating the number of data bits transmitted per second.
Getting Started with Arduino Input/Output: A Beginner's Guide
Within the main program loop, the Serial.print() function can be used to send information to the serial port. Serial.println() functions similarly to Serial.print() but will print on a new line.
Line 27 in the code adds the current button state value to the serial port.
Getting Started with Arduino Input/Output: A Beginner's Guide
Therefore, when the serial monitor is open, you can see the values detected by Arduino on pin 2 in real-time. The serial monitor is very useful for troubleshooting, allowing you to easily compare expected results with what Arduino actually executes.
You can also use serial communication for more functions like communication between devices, etc. For more details, please refer to the Arduino reference documentation.
Getting Started with Arduino Input/Output: A Beginner's Guide
Next, you will send information to the Arduino board using analog inputs. The pins that begin with A, located opposite the digital input/output pins, connect to Arduino’s analog-to-digital converter (ADC). It can convert analog signals ranging from 0 volts to 5 volts into numbers ranging from 0 to 1023.
Getting Started with Arduino Input/Output: A Beginner's Guide
To create this analog signal, you can use a variable resistor, which changes its resistance value when acted upon. For example, you can turn the knob on a potentiometer, press or bend a force-sensitive resistor (FSR), or change the exposure of a photoresistor.
Getting Started with Arduino Input/Output: A Beginner's Guide
Insert a small potentiometer into three rows of the breadboard, connecting the outer pins to power and ground, and the middle pin to analog pin A0.
Getting Started with Arduino Input/Output: A Beginner's Guide
Connect an LED to pin 9 so that you can control the brightness of the LED using pulse width modulation (PWM).
Getting Started with Arduino Input/Output: A Beginner's Guide
The built-in example code for this circuit can be found in the Arduino software under 03 Analog > AnalogInOutSerial.
Getting Started with Arduino Input/Output: A Beginner's Guide
Getting Started with Arduino Input/Output: A Beginner's Guide
After uploading the code, open the serial monitor and observe the status of the LED as you turn the knob on the potentiometer. The values read from the analog input will be in the first column, and the brightness value of the LED will be in the second column.
Getting Started with Arduino Input/Output: A Beginner's Guide
This example program uses the map() function on line 39, which maps a range of numbers to another range of values. It has five parameters, including: the value to change, the lower limit of the current range, the upper limit of the current range, the lower limit of the target range, and the upper limit of the target range. Thus, this line of code sets a variable named outputValue to a number between 0 and 255, depending on the position of the potentiometer.
Getting Started with Arduino Input/Output: A Beginner's Guide
Lines 44 to 47 of the serial print commands list text labels (the content within quotes) as well as the values from the sensor input and the output to the LED.
By viewing these values simultaneously on the serial monitor, you can better understand how functions like map() work. Keep this in mind when writing your own code!
Getting Started with Arduino Input/Output: A Beginner's Guide
Many sensors work similarly to digital switches or analog inputs, allowing you to use the same code just explained. For example, you can replace the button with an infrared sensor, or replace the potentiometer with a photoresistor.
If you watched the previous episode’s introduction and have made it this far, you have now mastered most of the basic building blocks of Arduino programs. When designing your projects, ask yourself what the inputs are, what the outputs are, and whether they are analog or digital. Then you can mix and match built-in examples accordingly and start your prototyping. Arduino has many more built-in examples for you to explore slowly, unleash your creativity, and achieve more innovative ideas.

About this video series

Electronic design is profound and can be somewhat challenging for beginners. This series of videos aims to introduce some entry-level techniques and skills, including the basics of circuit design, simple programming, etc., allowing beginners to start from the fundamentals and gradually advance to more complex concepts, discovering more fun in the world of electronic design while also allowing electronic design enthusiasts to review and learn.
About Becky Stern
Getting Started with Arduino Input/Output: A Beginner's Guide
Becky Stern is a maker with years of experience designing electronic projects, and creating and sharing are her two main hobbies. Before becoming an independent entrepreneur, she served as a product manager at Instructables (Autodesk) and director of wearable electronics at Adafruit. Her work has been featured in well-known media such as BBC, CNN, Business Insider, Forbes, and Science Friday. She has created hundreds of online DIY tutorials and videos about technology and crafting, aiming to share her knowledge and help fellow electronic design enthusiasts learn and communicate with each other.
Like the video series “Getting Started with Becky in the World of Electronics”? Please follow, subscribe, like, bookmark, and share, and stay tuned for the next episode!

Getting Started with Arduino Input/Output: A Beginner's Guide Related Products:

Getting Started with Arduino Input/Output: A Beginner's Guide

Arduino

A000066

Getting Started with Arduino Input/Output: A Beginner's Guide

Adafruit

2488

Getting Started with Arduino Input/Output: A Beginner's Guide

SparkFun

DEV-15123

Getting Started with Arduino Input/Output: A Beginner's Guide

DFRobot

FIT0096

Getting Started with Arduino Input/Output: A Beginner's Guide

Adafruit

1311

Getting Started with Arduino Input/Output: A Beginner's Guide

SparkFun

COM-09806

Getting Started with Arduino Input/Output: A Beginner's Guide

Advanced Photonix

PDV-P8103

Getting Started with Arduino Input/Output: A Beginner's Guide

Seeed Technology

102010026

Getting Started with Arduino Input/Output: A Beginner's Guide

Adafruit

1957

Getting Started with Arduino Input/Output: A Beginner's Guide

Same Sky

TS02-66-60-BK-160-LCR-D

Getting Started with Arduino Input/Output: A Beginner's Guide

Interlink Electronics

30-81794

Getting Started with Arduino Input/Output: A Beginner's Guide

Adafruit

189

Getting Started with Arduino Input/Output: A Beginner's Guide Editor’s Note

Mastering the methods of basic input/output with Arduino enables designers to build interactive, responsive system prototypes that allow for a more intuitive understanding and testing of design concepts. This also helps designers optimize the functionality and performance of prototypes, enhancing the user experience and providing a solid foundation for subsequent development and iteration of prototypes. After watching this issue, what thoughts or questions do you have? We welcome your comments and sharing!

Getting Started with Arduino Input/Output: A Beginner's Guide

Leave a Comment

×