The Up-Down SensorThe Up-Down Sensor

A simple Arduino microcontroller flasher can easily be turned into an Up-Down Sensor by adding a tilt control switch. As shown in Figure 8-3, the tilt control sensor is mounted on the MakerShield. When the tilt control switch is in the horizontal position, both the red and green LEDs will flash. Placing the MakerShield on its side will rotate the tilt control switch to vertical. The red LED will turn off and the green LED will be on but not flashing. Rotating the MakerShield back to the horizontal position causes the red and green LEDs to resume flashing. You can use either the Fritzing diagram shown in Figure 8-4 or the circuit schematic diagram in Figure 8-5 to build the Up-Down Sensor.

TECH NOTE

Placing another tilt control switch on the MakerShield in a vertical position can provide back and forth detection for robotics projects.

The Up-Down Sensor concept diagram

Figure 8-3. The Up-Down Sensor concept diagram

The Up-Down Sensor Fritzing diagram

Figure 8-4. The Up-Down Sensor Fritzing diagram

The Up-Down Sensor circuit schematic diagram

Figure 8-5. The Up-Down Sensor circuit schematic diagram

You can build the Up-Down Sensor on a MakerShield, as shown in Figure 8-6. The MakerShield allows you to carry it in a shirt pocket, computer bag, or purse for convenience. Example 8-1 can be uploaded to the Arduino after entering the code into the IDE’s text editor screen.

Example 8-1. Up-Down Sensor sketch

/*
  Up-Down Sensor with Flashing LEDs

  Flashes green and red LEDs at pin 8 when the tilt control
  switch attached to pin 3 is tilted. The green LED wired to
  pin 8 turns turns solid when no tilt condition is detected.

  05 Feb 2013
  Don Wilcher

 */

// constants won't change; they're used here to
// set pin numbers:
const int tiltPin = 3;     // the number of the tilt control switch pin
const int ledPin = 8;      // the number of the LED pin

// variables will change:
int tiltState = 0;         // variable for tilt control switch status

void setup() {
  pinMode(ledPin, OUTPUT);
  // initialize the tilt control switch pin as an input:
  pinMode(tiltPin, INPUT);
}

void loop(){
  // read the state of the tilt control switch value:
  tiltState = digitalRead(tiltPin);

  // check if the tilt control switch contacts are closed
  // if they are, the tiltState is HIGH:
  if (tiltState == HIGH) {
    // turn Red LED on;
    digitalWrite(ledPin, HIGH);
    // wait 100ms:
    delay(100);
    // turn LED off:
    digitalWrite(ledPin,LOW);
    // wait 100ms:
    delay(100);
  }
  else {
    // turn LED off:
   digitalWrite(ledPin, LOW);
  }
}

After uploading the Up-Down Sensor sketch to the Arduino microcontroller, orient the MakerShield so that the tilt control switch is horizontal. The red and green LEDs should be flashing. Next, rotate the MakerShield onto its side. The red LED will be off and the green LED will be on. Experiment with different MakerShield orientations and notice the response of the LEDs. Remember to record your observations in your lab notebook!

TROUBLESHOOTING TIP

If the LEDs don’t turn on after uploading the sketch to the Arduino microcontroller, check that the correct output pin is being used. Also, check to see that the LEDs are wired up properly, with the short wires connected to GND.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *