Week 3 Arduino Exercises

These exercises were a little harder to wrap my head around than last week because I don’t quite understand how the pull-up resistor works.  In the exercise it says that the digitalRead pin has a direct connection to ground when the button is pressed, so it ignores the small amount of electricity coming from through the resistor.  This seems like a strange way to explain it however because electricity doesn’t run from ground to ground so it’s not really directly sensing that the other ground is activated.  The way I understand it is that the small amount of electricity from the resistor is only going into the digitalRead pin which is just enough to return a HIGH reading.  But when the button is pressed, the current is split between to the two grounds, so the digitalRead pin only feels half the original current which puts it within the threshold to return a LOW reading.  I really do want to make sure I understand this so I’ll be sure to ask about it in class.

For the final exercise I did the two momentary switches controlling one LED.  I managed to set it up so that both buttons were using the pull-up resistors integrated into the Arduino pins.

// constants won't change. They're used here to set pin numbers:
const int buttonPin2 = 2; // the number of the pushbutton pin
const int buttonPin3 = 3;
const int ledPin = 13; // the number of the LED pin

// variables will change:
int buttonState2 = 0; // variables for reading the pushbutton status
int buttonState3 = 0;

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin3, INPUT_PULLUP);

void loop() {
// read the state of the pushbutton values:
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);

// check if the pushbutton 2 is pressed. If it is, the buttonState is LOW:
if (buttonState2 == LOW) {
// turn LED on:
digitalWrite(ledPin, HIGH);

// check if the pushbutton 3 is pressed. If it is, the buttonState is LOW:
if (buttonState3 == LOW) {
// turn LED off:
digitalWrite(ledPin, LOW);

One thought on “Week 3 Arduino Exercises”

  1. Hi Wes, maybe I can help explain more about the pull up resistor circuit in the exercise. There aren’t two grounds– the microcontroller pin is in INPUT mode so it’s floating until you tie it to either power or ground. Electricity takes the path of least resistance. The resistor to 5V provides a weaker signal to the pin than the ground connection, whose resistance is ~zero when the button is pressed (and not connected when not pressed, which equates to ~infinite resistance). We will definitely go over this in class as well.


Comments are closed.