You are on page 1of 22

f Login (/account/login/) | Sign Up (/account/register/)

Input/Output
(/)

with bekathwia (/member/bekathwia/) in ARDUINO CLASS (/class/Arduino-Class/)

« Previous Lesson (/lesson/Your-First-Experiments/) Download

Next Lesson » (/lesson/Skills-Infusion/)

(https://cdn instructables com/FHK/A44E/J3KFAPWS/FHKA44EJ3KFAPWS LARGE jpg)

So far you've learned to control LEDs with code, which is one use of Arduino's
outputs. This lesson builds on outputs by adding inputs. Your Arduino board can be
programmed to listen to electrical signals and take actions based on those inputs.
We'll put together a digital input circuit with a switch, and an analog input circuit with
a potentiometer (variable resistor).
A switch is a mechanical device that connects or breaks a circuit, often using a lever
or button. Your tiny tactile pushbutton is just one member of a very large and diverse
family. To learn much more, check out the switches lesson
(https://www.instructables.com/lesson/Switches/) in the Instructables Electronics
class (https://www.instructables.com/class/Electronics-Class/).

A variable resistor is a component with a changing electrical resistance. In addition to


the potentiometer (knob) used in this lesson, you may seek out other variable resistors
to try, including but not limited to: pressure sensors (force sensitive resistor
(https://learn.adafruit.com/force-sensitive-resistor-fsr/overview?view=all#overview)
aka FSR), light dependent resistors (https://learn.adafruit.com/photocells/overview?
view=all) (LDR aka photoresistor), and analog temperature sensors
(https://learn.adafruit.com/tmp36-temperature-sensor/overview?view=all).

Supplies
To follow along with this lesson you will need:

Computer running Arduino software (https://www.arduino.cc/en/Main/Software)


Some of the items from the Adafruit Arduino Uno Budget Pack
(https://www.adafruit.com/products/193)
Arduino Uno board (https://www.adafruit.com/products/50)
USB A-B cable (https://www.adafruit.com/products/62)
Half-sized breadboard (https://www.adafruit.com/products/64)

Breadboard wires (https://www.adafruit.com/products/153)


10K potentiometer (https://www.adafruit.com/products/356)
1 small pushbutton (https://www.adafruit.com/products/367)
1 red diffused 5mm LED (https://www.adafruit.com/products/299)
1 220-1K ohm resistor (https://www.adafruit.com/products/2780) (any value
within range OK)
1 10K ohm resistor (https://www.adafruit.com/products/2784)

Plastic mounting plate for breadboard and Arduino


(https://www.adafruit.com/products/275)
Small DC motor (https://www.adafruit.com/products/711)
(https://www.adafruit.com/products/711)PN2222 transistor
(https://www.adafruit.com/product/756)
(https://www.adafruit.com/product/756)1N4001 diode
(https://www.adafruit.com/products/755)

Digital Input

(https://cdn instructables com/FLP/QAU8/J3KFAPZ0/FLPQAU8J3KFAPZ0 LARGE jpg)

Let's get ready to wire up a new circuit. Grab some breadboard wires, a red LED, 1K
resistor (brown-black-red-gold), 10K resistor (brown-black-orange-gold), and a small
pushbutton from your kit. Give your 10K resistor the same elbow+trim treatment as
your other tidy resistors.
First, connect the LED to Arduino pin 13 like you did in your rst circuit, with a wire
from pin 13 to the positive lead of your LED, and a 1K resistor connecting the LED's
negative lead to ground.
Next, plug in the pushbutton so that it straddles the center dividing line of your
breadboard. It should click into place snugly. Connect the 10K resistor from one lead
of the switch to the breadboard's 5V power rail. Connect a wire to this same row and
plug it into Arduino pin 2.
Connect the switch's diagonal lead to ground.

Button Iniciar simulação Código

Finishing

Find this circuit on Tinkercad (https://www.tinkercad.com/things/9IFe140QD43-


button)
Click "Start Simulation" in the Tinkercad Circuits module and try clicking (and holding)
the pushbutton to see what the code does. Click the "Code" button to see the sketch.

You can nd this example in the Arduino software by navigating to File -> Examples -
> 02.Digital -> Button. Open it on your computer, and upload it to your Arduino Uno
board. The LED should light up, but turn off whenever you press the pushbutton.

Follow along as we explore the code in more detail.


SOBRE
Button OS COOKIES NESTE SITE

Os cookies são importantes para o correto funcionamento de um site.


Para melhorar a sua experiência,
Button.ino Button.txt utilizamos cookies para lembrar detalhes
layout.png schematic.png
de início de sessão, recolher estatísticas para otimizar a funcionalidade do
1 ▾ /*
2
site e apresentar conteúdo de acordo com os seus interesses. Clique em
Button
3 Concordar e prosseguir para aceitar os cookies e avançar diretamente
4 Turns
para on and
o site off a em
ou clique light emitting
Visualizar diode(LED)deconnected
as definições to digital
cookies para ver pin 13,
5 when pressing a pushbutton attached to pin 2.
descrições detalhadas quanto aos tipos de cookies e escolher se pretende
6
7 aceitar determinados cookies enquanto estiver no site.
The circuit:
8 - LED attached from pin 13 to ground
9 - pushbutton attached to pin 2 from +5V
10 - 10K resistor attached to pin 2 from ground
11 CONCORDAR E PROSSEGUIR
12 - Note: on most Arduinos there is already an LED on the board
13 attached to pin 13. VISUALIZAR DEFINIÇÕES DE COOKIES
14
15 created 2005
16 by DojoDave <http://www.0j0.org>
17 modified 30 Aug 2011
18 by Tom Igoe
19 Politica de Privacidade Accionado por:

The rst lines of this program introduce constants, which are similar to variables in
that they store a piece of information. However as you might infer, constants don't
change throughout your program, and are therefore great for things like pin numbers.
They take up less memory space than variables. Row 39 con gures Arduino pin 2 as
an input, so we can "listen" to the electrical state of the pushbutton.

In the main loop, a function called digitalRead(); checks the state of pin 2 (which will
be either 5V aka HIGH or ground aka LOW), and stores that state in a variable called
buttonState. Row 48 contains an if statement that checks to see if buttonState is
HIGH ( == is a comparison operator (https://www.arduino.cc/en/Reference/If), not to
be confused with = , which is an assignment operator
(https://www.arduino.cc/en/Reference/Assignment)). If the condition is met, the
digitalWrite(ledPin, HIGH); command is executed. If not, the code contained inside the

else { is executed instead: digitalWrite(ledPin, LOW); . If statements can exist alone, or


with one or more else statements (https://www.arduino.cc/en/Reference/Else).

Have you noticed that your pushbutton circuit performs the opposite action from that
described in the code? I did that on purpose to stretch your mental muscles; let's
discuss the switch circuit some more. At rest, the switch leads are not connected to
one another. Pin 2 is connected through a beefy 10K resistor to 5V. When the button
is pressed, the switch leads are connected, which allows pin 2 to be connected to
ground, with no resistor. Since electricity takes the path of least resistance, the pin will
sense the connection to ground strongly, and ignore the weak (10K) connection to 5V.
But when no other signal is present (when the switch isn't being pressed), that weak
connection to 5V is all the pin can sense. So the resistor is "pulling the pin up" to 5V,
and so it's called a pull-up resistor. Without one, pin 2 would be not connected to
anything until the button is pressed. This is called " oating", and can result in random
noise from static electricity and electromagnetic interference. Similarly a resistor can
be used to tie a pin to ground, which is called a pull-down resistor.

So to change the function of the button, you can either change the wiring of your
circuit, or change the code. The latter is less work in this case, but it's important to
remember that might not always be the case when you're building projects on your
own. Edit lines 47 (comment) and 48 (if statement) to say LOW instead of HIGH:

void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin); // check if the pushbutton is pressed.

// if it is, the buttonState is LOW:


if (buttonState == LOW) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}

Upload the updated sketch to your Arduino Uno board, and check that the button
now turns the LED on instead of off.

Arduino pins have built-in pull-up resistors on many of the pins (tiny ones, inside the
chip just for this purpose), and you can access one by enabling it in the setup:

pinMode(buttonPin, INPUT_PULLUP);

Change this line of code in your sketch and remove the resistor from your circuit.
Upload the code; its behavior should remain the same.

The Serial Monitor


Keeping track of everything going on in your program can be an uphill battle. The
serial monitor is a way to check in on different spots in your code by reporting back to
the computer over the USB cable.

Digital Read Serial Iniciar simulação Código

Texto

1 /*
2 DigitalReadSerial
3 Reads a digital input on pin 2, prin
4
5 This example code is in the public d
6 */
7
8 // digital pin 2 has a pushbutton att
9 int pushButton = 2;
10
11 // the setup routine runs once when y
12 void setup() {
13 // initialize
Finishing serial communication
14 Serial.begin(9600);
15 // make the pushbutton's pin an inp
16 pinMode(pushButton, INPUT);
17 }
18
//

Find this circuit on Tinkercad (https://www.tinkercad.com/things/f8aWEyOM4IE-


digital-read-serial)
This circuit uses the same button con guration as the previous digital input circuit, but
just doesn't use the LED. Load up the code from this module or nd it in the Arduino
software by navigating to File -> Examples -> 01.Basics -> DigitalReadSerial. Upload
this code to your board and click the Serial Monitor button in the upper right of the
sketch window. You can also open the virtual serial monitor in the Tinkercad Circuits
module above (button at the bottom of the code editor).

To create a serial connection, you should use Serial.begin(); inside your setup. The
number 9600 is the baud rate, or data speed, in bits per second (bps). Inside the main
program loop, you can use Serial.print(); to send information to the serial port.
Serial.println(); does the same thing, but prints on a new line. Line 24 in our code
prints the current value of buttonState (HIGH aka 1 or LOW aka 0) to the serial port.
So with the serial monitor open, we have a live scrolling view of whatever the Arduino
senses at pin 2. The serial monitor is exceptionally handy when troubleshooting, since
you can easily compare what you think should be happening to what the Arduino is
doing. You can also use serial communication to talk between devices and much more,
which you can read about in the Arduino reference
(https://www.arduino.cc/en/Reference/Serial).

Analog Input

(https://cdn instructables com/FEU/496J/J3KFAQ0L/FEU496JJ3KFAQ0L LARGE jpg)

To sense a gradually changing electrical signal, we'll use Arduino's analog inputs,
located on the left side of the board. These special pins are connected to the
Arduino's analog to digital converter (ADC), equipped to convert an analog signal
between 0V and 5V into a range of numbers from 0-1023 (zero counts as a value).
Another powers-of-two size, 1024 is 2^10 bytes, aka 1KB
(https://en.wikipedia.org/wiki/Kilobyte). Grab one of the blue potentiometers from your
kit and plug it into three rows on your breadboard (with your USB disconnected).
Analog In, Serial Out Iniciar simulação Código

Finishing

Find this circuit on Tinkercad (https://www.tinkercad.com/things/eTYPBoTCSGN-


analog-in-serial-out)

Connect the center pin of the potentiometer to Arduino analog pin A0, and the other
two pins to power and ground respectively. Move your LED's control wire from pin 13
to pin 9, so we can use PWM.
You can get the code from the Tinkercad Circuits module as you have for previous
lessons, or nd the example by navigating to File -> Examples -> 03.Analog ->
AnalogInOutSerial.

Plug in your USB cable and upload the sketch to your Arduino Uno board. Open the
serial monitor and observe it and the LED as you twist the potentiometer.
The values read by the analog input are printed in the rst column, and the brightness
values applied to the LED are printed in the second column.

Let's take a closer look at the main loop of this program:


This sketch uses the map(); function on line 39, which takes one range of numbers
and massages it into another range. It takes ve arguments: the value to be changed,
the lower bound of the value's current range, the upper bound of the value's current
range, the lower bound of the target range, and the upper bound of the target range.
So this line of code sets a variable outputValue to a number between 0 and 255
depending on the position of the potentiometer.

The serial printing commands on lines 44-47 print text labels (inside quotes) and the
values incoming from the sensor and outgoing to the LED. Seeing these numbers
change together in the serial monitor can really help you understand how functions
like map() (https://www.arduino.cc/en/Reference/Map); work. Keep this in mind when
composing and troubleshooting your own sketches!

A Moment for Motors


(https://cdn instructables com/FDR/2QOW/J3KFAQ2M/FDR2QOWJ3KFAQ2M LARGE jpg)

The exact same code used to brighten and dim the LED in the previous circuit can also
be used to control the speed of a simple DC motor. This basic motor needs a few
extra components to help control it: a small resistor (use your 1K or try a 100 ohm
marked brown-black-brown-gold), a NPN transistor (we're using a PN2222), and a
diode (1N4001). The resistor is used to protect the Arduino pin from excessive current
draw. The diode prevents the transistor from dumping any blowback voltage
anywhere it shouldn't (something motors are prone to doing). The transistor acts like
an electronic valve, allowing current to ow between its collector and emitter
(PN2222 outer pins) in proportion to the signal it receives at the base (PN2222 center
pin). Transistors are handy for controlling a rather power hungry component with a
microcontroller pin, which can't deliver enough current directly.
Analog In, Serial Out, DC Motor Iniciar simulação Código

Finishing

Find this circuit on Tinkercad (https://www.tinkercad.com/things/7l0BoHp3X7A-


analog-in-serial-out-dc-motor)

Unplug your USB cable and build the circuit according to the diagram, minding the at
side of your transistor (faces away from the Arduino in this circuit), as well as the
stripe on your diode (on the side furthest from the transistor). If you're using a
different NPN transistor (like the 2N2222), your transistor pin connections may be
different than those pictured, so look up the datasheet to be sure you're making the
following connections:

Arduino pin 9 to transistor base through resistor


5V to transistor collector through diode
Ground to transistor emitter

Motor wires to transistor collector and 5V (either orientation)

Power up your board and see what effect turning the knob has on the speed of the
motor (use a piece of tape to make it easier to see the motor shaft spinning).
The motor recommended for this circuit draws less than 250mA, but a larger one
could require more power than your computer's USB port can deliver. To power
bigger motors, lots of LEDs, and other circuits that use more power, you'll need to use
an external power supply, such as an AC adapter or battery pack. Additionally, for any
larger of a motor, you'd also need a larger transistor. We'll learn how to calculate your
circuit's power needs in the next lesson, but by popular request, here's the same
motor circuit powered by an external 6V battery pack (separate power rails, common
ground):
Analog In, Serial Out, DC Motor with External Power Iniciar simulação

Texto

1 /*
2 Analog input, analog output, serial
3
4 Reads an analog input pin, maps the
5 and uses the result to set the pulse
6 Also prints the results to the seria
7
8 The circuit:
9 * potentiometer connected to analog
10 Center pin of the potentiometer go
11 side pins of the potentiometer go
12 * LED connected from digital pin 9 t
13 Finishing
14 created 29 Dec. 2008
15 modified 9 Apr 2012
16 by Tom Igoe
17
18 This example code is in the public d

Find this circuit on Tinkercad (https://www.tinkercad.com/things/9rJr3EylTCw-analog-


in-serial-out-dc-motor-with-external-power)

To learn more about transistors and use them in a project, check out the transistors
lesson (https://www.instructables.com/lesson/Transistors/) in the Instructables
Electronics Class (https://www.instructables.com/class/Electronics-Class/). To learn
more about motors and use them to build several fun robots, check out the
Instructables Robots Class (https://www.instructables.com/class/Robots-Class/).

You now know the basic building blocks of most Arduino programs, and have built a
few circuits demonstrating their utility. Great work!

Class Project
Share a photo of your finished project with the class!

Share a photo of your nished project with the class!

I Made It!

« Previous (/lesson/Your-First-Experiments/) Next » (/lesson/Skills-Infusion/)

Ask a Question
Search existing questions... 
Ask a Question

Motor not working?


Answered 7 Answers

arduino board?
Answered 1 Answer

So a transistor amplifies the power that is coming out of the pin?


Answered 1 Answer

Credit not given for finishing.?


Answered 3 Answers

pin doubt?
Answered 1 Answer

Combining sketches and wiring?


Answered 2 Answers

resistors?
Answered 1 Answer

Error message, usb draws too much current.?


Answered 1 Answer

Random Question?
Answered 1 Answer

nothing happened!?
Answered 14 Answers
F
More Questions

Categories About
Us
Technology (/technology/)
Workshop (/workshop/) Who We
Craft (/craft/) Are
Home (/home/) (/about/)
Food (/food/) Why
Play (/play/) Publish?
Outside (/outside/) (/create/)
Costumes (/costumes/) Jobs
(/topics/Positions-
available-
at-
Instructables/)
Contact
(/contact/)
Resources Find Us
Community
(/topics/)
Sitemap (http://www.instagram.com/instructables/)

(/sitemap/)
Help (/id/how-to-
(http://www.pinterest.com/instructables)
write-a-great-
instructable/)

(http://www.facebook.com/instructables)

(http://www.twitter.com/instructables)

© 2018 Autodesk, Inc. (http://www.autodesk.com)

Terms of Service (http://usa.autodesk.com/adsk/servlet/item?siteID=123112&id=21959721) |


Privacy Statement (http://usa.autodesk.com/adsk/servlet/item?siteID=123112&id=21292079) | Privacy settings |
Legal Notices & Trademarks (http://usa.autodesk.com/legal-notices-trademarks/)

You might also like