Digital Input, Analog Input, and Sensor Change Detection

Goal

This week I learned how to use Arduino to connect the physical world with code.

The main idea:

Input → Processing → Output

Example:

Button ↓ Arduino ↓ LED

A sensor collects information from the environment, Arduino processes the data, and then controls an output device.

Materials Used

Hardware Arduino Uno / Nano Breadboard USB cable LED 220Ω resistor Push button Potentiometer Photoresistor (LDR) Jumper wires Software Arduino IDE Serial Monitor

image.png

Lab 1

image.png

7905_1790052021.mp4

The interaction is:


Pushbutton
↓
Arduino UNO
↓
white LED / blue LED

code

int buttonPin = 2;

int led1 = 3;
int led2 = 4;

void setup() {

  pinMode(buttonPin, INPUT_PULLUP);

  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);

}

void loop() {

  int buttonState = digitalRead(buttonPin);

  if(buttonState == LOW) {

    digitalWrite(led1, HIGH);
    digitalWrite(led2, LOW);

  } 
  
  else {

    digitalWrite(led1, LOW);
    digitalWrite(led2, HIGH);

  }

}