Upload the Metal Checker Sketch

With the Metal Checker circuit built on the MakerShield, it’s time to upload the sketch. Example 11-1 operates the piezo buzzer using a small transistor. Here are the steps you’ll need to follow:

  1. Attach the Arduino to your computer using a USB cable.
  2. Open the Arduino software and type Example 11-1 into the software’s text editor.
  3. Upload the sketch to the Arduino.
  4. Touch the test probes together.

The Arduino will turn on the piezo buzzer. Now you’re ready to unlock the metal mysteries hiding in your house!

Example 11-1. The Metal Checker sketch

/*
  Metal Checker

  Turns on and off a piezo buzzer at pin 7 when metal is placed across
  the sense wires of the metal sensor circuit attached to pin 6.

  The circuit:
    * Piezo buzzer attached from pin 7 to ground
    * Metal Checker sensor attached to pin 7
    * 1KΩ fixed resistor attached from pin 6 to ground

  March 2013
  by Don Wilcher


*/

// set pin numbers:
const int MSensePin = 6;     // the number of the metal sense pin
const int PBuzzerPin =  7;   // the number of the piezo buzzer pin

// variables will change:
int MetalStatus = 0;         // variable for the metal sense status

void setup() {
  // initialize the LED pin as an output:
  pinMode(PBuzzerPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(MSensePin, INPUT);
}

void loop(){
  // read the state of the metal sense value:
  MetalStatus = digitalRead(MSensePin);

  // check if metal is present
  // if it is, the MetalStatus is HIGH:
  if (MetalStatus == HIGH) {
    // turn piezo buzzer on:
    digitalWrite(PBuzzerPin, HIGH);
  }
  else {
    // turn MetalStatus off:
    digitalWrite(PBuzzerPin, LOW);
  }
}

THE TRANSISTOR

The transistor is a small electronic component that can be used as an electronic switch or an amplifier. There are two types of transistors: NPN and PNP. These transistor types can be used as electronic switches and amplifiers: the current flow in the PNP transistor is opposite of that in the NPN transistor. The 2N3904 NPN transistor is being used as an electronic switch, replacing the mini pushbutton component of previous Arduino projects. To learn more about the transistor, read Charles Platt’s Make: Electronics (Maker Media, 2009).

TROUBLESHOOTING TIP

If the piezo buzzer doesn’t turn on, check your sketch pin assignments as well as the orientation of the transistor on the MakerShield mini breadboard.


Comments

Leave a Reply

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