[go: up one dir, main page]

0% found this document useful (0 votes)
98 views6 pages

9b - Input Pullup - Serial

This document describes using an internal pull-up resistor with a switch to monitor its state through serial communication. It connects a switch between an Arduino pin set to input pull-up and ground. When the switch is open, the pin reads high due to the internal pull-up. When closed, it is pulled low. The program prints the pin state and turns on an LED when the switch is closed.

Uploaded by

Hoang Nguyen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views6 pages

9b - Input Pullup - Serial

This document describes using an internal pull-up resistor with a switch to monitor its state through serial communication. It connects a switch between an Arduino pin set to input pull-up and ground. When the switch is open, the pin reads high due to the internal pull-up. When closed, it is pulled low. The program prints the pin state and turns on an LED when the switch is closed.

Uploaded by

Hoang Nguyen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Input Pullup – Out Serial (use internal resistor)

This example demonstrates the use of INPUT_PULLUP with pinMode(). It monitors


the state of a switch by establishing serial communication between your Arduino and
your computer over USB.

Additionally, when the input is HIGH, the onboard LED attached to pin 13 will turn
on; when LOW, the LED will turn off.

Atmega328
Microcontroller

0 - pinMode(3, INPUT);
1 - pinMode(3,INPUT_PULLUP)
1

Internal 0
Resistor
- 10k

Switch or Button
Hardware Required
 Arduino Board
 A momentary switch, button, or toggle switch
 breadboard
 hook-up wire

Circuit

Connect two wires to the Arduino board. The black wire connects ground to one leg
of the pushbutton. The second wire goes from digital pin 2 to the other leg of the
pushbutton.

Pushbuttons or switches connect two points in a circuit when you press them. When
the pushbutton is open (unpressed) there is no connection between the two legs of the
pushbutton. Because the internal pull-up on pin 2 is active and connected to 5V, we
read HIGH when the button is open. When the button is closed, the Arduino reads
LOW because a connection to ground is completed.
Schematic
Code
In the program below, the very first thing that you do will in the setup function is to
begin serial communications, at 9600 bits of data per second, between your Arduino
and your computer with the line:

Serial.begin(9600);

Next, initialize digital pin 2 as an input with the internal pull-up resistor enabled:

pinMode(2,INPUT_PULLUP);

The following line make pin 13, with the onboard LED, an output :

pinMode(13, OUTPUT);

Now that your setup has been completed, move into the main loop of your code.
When your button is not pressed, the internal pull-up resistor connects to 5 volts. This
causes the Arduino to report "1" or HIGH. When the button is pressed, the Arduino
pin is pulled to ground, causing the Arduino report a "0", or LOW.

The first thing you need to do in the main loop of your program is to establish a
variable to hold the information coming in from your switch. Since the information
coming in from the switch will be either a "1" or a "0", you can use an int datatype.
Call this variable sensorValue, and set it to equal whatever is being read on digital
pin 2. You can accomplish all this with just one line of code:

int sensorValue = digitalRead(2);

Once the Arduino has read the input, make it print this information back to the
computer as a decimal (DEC) value. You can do this with the command Serial.println()
in our last line of code:

Serial.println(sensorValue, DEC);

Now, when you open your Serial Monitor in the Arduino environment, you will see a
stream of "0"s if your switch is closed, or "1"s if your switch is open.
The LED on pin 13 will illuminate when the switch is HIGH, and turn off when
LOW.

void setup() {
  //start serial connection
  Serial.begin(9600);
  //configure pin 2 as an input and enable the internal pull-up resistor
  pinMode(2, INPUT_PULLUP);
  pinMode(13, OUTPUT);

void loop() {
  //read the pushbutton value into a variable
  int sensorVal = digitalRead(2);
  //print out the value of the pushbutton
  Serial.println(sensorVal);

  // Keep in mind the pull-up means the pushbutton's logic is inverted. It
goes
  // HIGH when it's open, and LOW when it's pressed. Turn on pin 13 when
the
  // button's pressed, and off when it's not:
  if (sensorVal == HIGH) {
    digitalWrite(13, LOW);
  } else {
    digitalWrite(13, HIGH);
  }
}

You might also like