[DIGITAL INPUT DEVICES]
Pushbuttons are switches that make an electrical connection when you push them.
BigButtonSwitchBrick2.pngMagneticSwitchBrick200.jpgTiltMercuryBrick200.pngPIR_Motion_Sensor-Brick-200.jpg
There are many different Input Devices that are really switches.. but many of them look different and have different ways they are operated.

EXAMPLES (Left to Right above):

OK, how do we hook these up and make them work?? We use either the YourDuinoRobo1 with 3-pin connectors built-in or a Sensor Shield plugged on top of a YourDuino328, YourDuino-UNO or other Arduino type board. Or a MEGA Sensor Shield plugged on top of a YourDuinoMEGA. Then we use Cables, like this:

LED-Brick-White1Sys.jpgSensorShieldV4-TK1-crop-512.jpg
The left photo shows the White LED brick plugged into the Sensor Shield with a 3-wire Cable. Details of the Sensor Shield are on the right. (More details HERE).

Hookup, and Software ...

OK, let's hook up a switch and try it out...


  • Connect the "Big Pushbutton" with a 3-wire cable to Digital I/O #3. Plug the "latched" end into the pushbutton brick and the flat end onto the Robo1 or Sensor Shield #3. (Make sure the White wire is connected to signal "S"). The labels "GVS" mean Ground(black), Voltage(red) and Signal(white).
  • Connect the "White LED" to Digital I/O #11 with a 3-wire cable in the same way.
  • Start the Arduino IDE, and make sure you have the right Board and Serial Port selected by the "Tools" menu.
  • Load and run the "Blink" program to make sure everything is set up correctly. The White LED should blink on and off every second.
  • Get a New blank IDE window (File > New) and copy and paste the following program (the part between the thin horizontal lines). Click "Verify" to make sure it's OK. Then click "Upload". You should see a message: "Done Uploading".

NOTE: Take your time and read the Software Sketches slowly. There are some comments to tell you what the lines mean. And there will be a separate page with a more detailed discussion of each Software Sketch.

(Continued after the program listing)


/* YourDuino Electronic Brick Test 1
 Simple Switch type INPUT DEVICES
 terry@yourduino.com */

/*-----( Declare Constants )-----*/
#define SWITCHPIN 3
#define LEDPIN    11

/*-----( Declare Variables )-----*/
int  switch_state;  /* Holds the last state of the switch */

void setup()   /*----( SETUP: RUNS ONCE )----*/
{
  pinMode(LEDPIN, OUTPUT);

}/*--(end setup )---*/


void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
{

  switch_state = digitalRead(SWITCHPIN);  
  if (switch_state == HIGH)
  {
    digitalWrite(LEDPIN, HIGH);
  }  
  else
  {
    digitalWrite(LEDPIN, LOW);
  }
}/* --(end main loop )-- */

/* ( THE END ) */






Pushbutton Action:


The action you should see is simple: push the button and the LED will light.

We didn't need a computer to do that !

But what if you suddenly need the LED to be ON and when the pushbutton is pressed, turn OFF??

You can redesign that with one keystroke. Find the line:

if (switch_state == HIGH) and add a "!" exclamation point before "HIGH", like this:

if (switch_state == ! HIGH)

This says "if switch_state == NOT HIGH" and inverts the logic of your program. Try it out...

Upload that and test it.... OK? Now change it back, Upload and test..

Other Simple Switches:


Your Pushbutton should now turn on the LED. Let's try some other simple switches:

Tilt Switch:

Plug this Electronic Brick on the end of the cable that had the Pushbutton. Tilt the brick back and forth so that you can see the small mercury drop move back and forth; you should also see the LED turn on and off. This is another "Simple Switch".

"Magnetism"

(If you have this brick.. not included in the Starter Set). Plug this Electronic Brick on the end of the cable that had the Pushbutton. Bring a magnet close to the bar at the end of the brick; ; you should also see the LED turn on and off. This is another "Simple Switch".


Simple Switch with Serial Monitor Output":


Here's the same software Sketch , but with 3 added lines to send the results to the "Serial Monitor" program. Save your Sketch if you made changes you want to keep, get a new blank window and copy/paste this:

/* YourDuino Electronic Brick Test 2
 Simple Switch type INPUT DEVICES
 with Serial Monitor Output
 terry@yourduino.com */

/*-----( Declare Constants )-----*/
#define SWITCHPIN 3
#define LEDPIN    11

/*-----( Declare Variables )-----*/
int  switch_state;  /* Holds the last state of the switch */

void setup()   /*----( SETUP: RUNS ONCE )----*/
{
  pinMode(LEDPIN, OUTPUT);
  Serial.begin(9600);

}/*--(end setup )---*/


void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
{
  Serial.print("SWITCH IS - ");
  switch_state = digitalRead(SWITCHPIN);  
  if (switch_state == HIGH)
  {
    digitalWrite(LEDPIN, HIGH);
    Serial.println("HIGH");
  }  
  else
  {
    digitalWrite(LEDPIN, LOW);
    Serial.println("LOW");    
  }
  delay(250); /* Wait a bit to see display */
}/* --(end main loop )-- */

/* ( THE END ) */



Upload and run this version. Then click the "Serial Monitor" (rightmost button on IDE). Push the button or activate your switch. You should see the printed output scroll up the Serial Monitor screen, telling you if the switch is ON or OFF.

"Simple" Switch - Motion Detector:


PIR-Jumpers-450.jpgLet's try out your Motion Detector brick. Connect it where the pushbutton was, on I/O Pin 3. WAIT for about 2 minutes for it to stabilize. Run the above program while you move a hand or body across the field of view of the detector. You should see the LED and Serial Monitor output show the result from the Motion Detector.

NOTE: The same detector is also available as a plain module with 3 pins (Right photo). In that case use jumper wires to connect the (G)round, (Voltage), (S)ignal.

PIR-Pot-450.jpgHINT: The detector has a small (usually orange) adjustment on its side. This sets how long the alarm is active once it detects motion. For this test, set it fully counter-clockwise. (Photo). Later if you wish it to stay active for a longer time after detecting movement (like you have it turn on the room lights) you can turn the adjustment more clockwise.

Simple Alarm System


If you have that working OK, let's make an alarm system. All we need to add is the buzzer to the LED we already have, and add a little code to our software Sketch.

Connect the buzzer brick with a cable to I/O #10.

Again, save your program if you've made changes, get another blank window and copy and paste the following Sketch:

/* YourDuino Electronic Brick Test 3
 Simple Switch type INPUT DEVICES
 INTRUDER ALARM
 with Serial Monitor Output
 terry@yourduino.com */

/*-----( Declare Constants )-----*/
#define SWITCHPIN      3
#define LEDPIN        11
#define BUZZERPIN     10

/*-----( Declare Variables )-----*/
int  switch_state;  /* Holds the last state of the switch */
int  buzzer_state;  /* Buzzer ON or OFF */

void setup()   /*----( SETUP: RUNS ONCE )----*/
{
  pinMode(LEDPIN, OUTPUT);
  pinMode(BUZZERPIN, OUTPUT);
  buzzer_state = false;    
  Serial.begin(9600);

}/*--(end setup )---*/


void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
{
  Serial.print("ALARM IS - ");
  switch_state = digitalRead(SWITCHPIN);  
  if (switch_state == HIGH)
  {
    digitalWrite(LEDPIN, HIGH);    
    Serial.println("GOING OFF ! !");

    buzzer_state = ! buzzer_state;
    if (buzzer_state == 1) 
    {
      digitalWrite(BUZZERPIN, HIGH);  
    }
    else  
    {
      digitalWrite(BUZZERPIN, LOW);  
    }    
  }  
  else /* The switch is OFF */
  {
    digitalWrite(LEDPIN, LOW);
    digitalWrite(BUZZERPIN, LOW);  
    buzzer_state = false;  
    Serial.println("NOT ACTIVE");    
  }
  delay(250); /* Wait a bit to see display and listen to buzzer*/
}/* --(end main loop )-- */

/* ( THE END ) */





Upload and run this version. When the alarm is activated, the LED should light and the buzzer should go on and off until the motion detector becomes inactive again.

Conclusion of this section:

We have used 3 or 4 different Digital Input Devices, two Digital Output Devices and sent print to the Serial Monitor. We've made a simple alarm system.

You can use these basics to design and put together many different devices.