Shop

Projects and Ideas

Turn on a Buzzer Based on a Motion Detector Reading

The Motion Detector can be used to make a simple alarm system by slightly modifying the VernierLibDemoMotionDetector sketch. This code turns on line 13 when the Motion Detector reads a distance less than 100 centimeters.
#include "VernierLib.h" 
VernierLib Vernier;
float distance = 0;// distance in cm 
void setup()
{
  Serial.begin(9600);
  Serial.print("Motion Detector reading...");
}

void loop()
{
  distance = Vernier.readMotionDetector();
  Serial.print(distance);
  Serial.println(" cm");
  delay(100);//delay a tenth of a second
}

Produce a Tone Out that Depends on Sensor Reading

A simple, but useful, sketch with analog sensors is to use the raw voltage from any analog sensor to control the frequency of the sound produced. This sketch is based on the Tone2 Tutorial. Read the voltage from the analog sensor in the normal way. Then use the Map and Tone commands to produce the tone. There are several ways that we have found this kind of system useful. First, for blind or low-vision students, it is great to have a tone produced that indicates the sensor reading. Also, sometimes when you are looking for a change in a sensor reading, it is easier to listen for a change than it is to stare at a meter or a graph. Note that you do not need to deal with calibration in this sketch, since it is the raw voltage from the sensor that controls the frequency. You can modify this sketch in many ways:
  • If you want to change the range of frequencies produced, change the last two numbers in the Map command, which are the lowest and highest frequencies to be produced.
  • If you have a sensor that puts out a voltage in a limited range, change the first two numbers in the Map command to the range of input voltages to be expected from the sensor.
  • As written, the program sends the analog voltage measure to the Serial Monitor. Because this is unnecessary; remove the Serial.println statement, if you like.
/*
VernierFrequency (v 2017.07)

This sketch produces a tone on a speaker connected to pin D9 of
the Arduino. It is based on the Tone 2 tutorial.
(see http://arduino.cc/en/Tutorial/Tone2).

Note that you do not deal with calibration here. Any Vernier
Analog sensor which uses the Sig1 (0 to 5 volt) line will work
if it is plugged into the BTA 1 connector.

If you want to change the range of frequencies, change the last two
numbers in the line:  
int thisPitch = map(sensorReading, 400, 1000, 120, 1500)
They are the minimum and maximum frequencies produced.
If you want to change the sensitiviy of the system, change the first
two numbers of the same line. They are the range of expected readings 
from the sensor.

See www.vernier.com/arduino for details.

*/
int outputPin=9;

void setup() 
{
  // initialize serial communications (for debugging only):
  Serial.begin(9600);
}

void loop() {
  // read the sensor:
  int sensorReading = analogRead(A0);
  // print the sensor reading so you know its range
  Serial.println(sensorReading);
  // map the analog input range (in this case, 400 - 1000 from the photoresistor)
  // to the output pitch range (120 - 1500Hz)
  // change the minimum and maximum input numbers below
  // depending on the range your sensor's giving:
  int thisPitch = map(sensorReading, 400, 1000, 120, 1500);

  // play the pitch:
  tone(outputPin, thisPitch, 10);
  delay(1);        // delay in between reads for stability
}

Triggering a Buzzer with a Button Press

The Vernier Arduino Interface Shield includes a general purpose button connected to digital line 12 on the Arduino. You can use this button to control an action in your project. Before this button will work, however, you need to add this line to the setup portion of your sketch:pinMode(12,INPUT_PULLUP);You read the status of the button with this line of code: int button = digitalRead(12);The value of button will be LOW when the button is pressed and HIGH when it is not pressed. The sample sketch, VernierLibTutorialDCUButtonPress, will turn on a buzzer connected to DCU line D1 when the general purpose button on the Vernier Arduino Interface Shield is pressed. The sketch assumes the buzzer is connected to lines D1 and GND on the DCU, and the DCU is plugged into the Digital 2 port on the Vernier Arduino Interface Shield. Note that this sketch will not work with a Digital Protoboard Adapter unless you wire a button to digital line D12 as there is no general purpose button on the adapter.
404: Not Found