You are on page 1of 16

MICROPROCESSOR

SYSTEMS
GROUP 8

PING ULTRASONIC RANGEFINDER


This sketch reads a PING ultrasonic rangefinder
and returns the distance to the closest object in
range
it sends a pulse to the sensor to initiate a reading,
then listens for a pulse to return.
The length of the returning pulse is proportional to
the distance of the object from the sensor.

MATERIALS NEEDED

Gizduino AT-Mega 644


Ping Ultrasonic Range Finder (4 pins)
Connecting Wires

THE CIRCUIT
Vcc connection of the PING attached to +5V
Gnd connection of the PING attached to ground
Trig connection of the PING attached to digital
pin 7
Echo connection of the PING attached to digital
pin 6

CODE
const int pingPin = 7;
const int echo = 6;
void setup() {
Serial.begin(9600);
}

void loop() {
long duration, inches, cm;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(echo, INPUT);
duration = pulseIn(echo, HIGH);

inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
}

long microsecondsToInches(long microseconds) {


return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds) {
return microseconds / 29 / 2;
}

MEMSIC 2125 DUAL-AXIS ACCELEROMETER

Converts the pulses output by the 2125 into


milli-g's (1/1000 of earth's gravity) and prints
them over the serial connection to the
computer.

MATERIALS NEEDED

Gizduino AT-Mega 644


MEMSIC 2125 Dual-Axis Accelerometer
Connecting Wires

THE CIRCUIT
X output of accelerometer to digital pin 2
Y output of accelerometer to digital pin 3
+V of accelerometer to +5V
GND of accelerometer to ground

CODE
const int xPin = 2;
const int yPin = 3;
void setup() {
Serial.begin(9600);
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
}

void loop() {
int pulseX, pulseY;
int accelerationX, accelerationY;
pulseX = pulseIn(xPin, HIGH);
pulseY = pulseIn(yPin, HIGH);
accelerationX = ((pulseX / 10) - 500) * 8;
accelerationY = ((pulseY / 10) - 500) * 8;

Serial.print(accelerationX);
Serial.print("\t");
Serial.print(accelerationY);
Serial.println();
delay(100);
}

You might also like