Arduino Data Sheets

Servo

Liam Brady

Servos are specialized motors that support precise control. Rather than spinning off to oblivion like a motor, servos let the designer specify exactly where they want the object to rotate to. Most common servos allow for 180º of rotation.

#include <Servo.h>
Servo myservo;

void setup() {
  myservo.attach(9);
}

void loop() {
  int val = analogRead(A0);
  val = map(val, 0, 1023, 0, 179);

  myservo.write(val);

  delay(15);
}

Push Button

Liam Brady

Push buttons are momentary switches; you press down on the button, and it completes the circuit. You release the button, and the circuit is once again open.

void setup() {
  Serial.begin(9600);
  pinMode(8, INPUT_PULLUP);
}

void loop() {
  if (digitalRead(8) == false) {
    Serial.println(“Button is pressed.”);
  }
  delay(100);
}

Potentiometer

Liam Brady

Potentiometers are variable resistors. Potentiometers have a knob that varies the output voltage (by varying the resistance as they are turned). The analog pins on the Arduino can measure this change.

void setup() {
  Serial.begin(9600);
}

void loop() {
  int val = analogRead(A0);

  Serial.println(val);

  delay(100);
}

Push Button

Liam Brady

Push buttons are momentary switches; you press down on the button, and it completes the circuit. You release the button, and the circuit is once again open.

void setup() {
  Serial.begin(9600);
  pinMode(8, INPUT_PULLUP);
}

void loop() {
  if (digitalRead(8) == false) {
    Serial.println(“Button is pressed.”);
  }
  delay(100);
}

PIR Motion Sensor

Liam Brady

PIR sensors (or passive infrared sensors) are motion sensors that measure subtle changes in infrared light in the room and register that change as movement. The processing is done on the chip and it outputs a current based on whether or not it detects motion.

int pin = 2;

void setup(){
  pinMode(pin, INPUT);

  pinMode(13, OUTPUT);
}

void loop(){
  int pirVal = digitalRead(pin);

  if (pirVal == 0) {
    digitalWrite(13, HIGH);
  } else {
    digitalWrite(13, LOW);
  }

  delay(100);
}

Photoresistor

Liam Brady

Photoresistors are resistance-based sensors that react to how much light they are being hit by. The more light present, the smaller the resistance across the sensor. The Arduino is able to measure the changing resistance to determine how much light is in the room.

void setup() {
  Serial.begin(9600);
}

void loop() {
  int val = analogRead(A0);

  Serial.println(val);
}

Force Sensor

Liam Brady

Force sensors are sensors that change resistance based on how much force is applied on it. The more force applied, the lower the resistance. Force can be measured using the analog pins on the Arduino.

void setup() {
  Serial.begin(9600);
}

void loop() {
  int val = analogRead(A0);

  Serial.println(val);
}