Arduino tutorial #03 | Using sensors with libraries

Contents

  1. Libraries
    1. Accelerometer
    2. Ultra sonic range finder

Libraries

There are several libraries specified to the each modules for Grove. These libraries are available at github repository of reedstudio. Please access to the repository and download them. This package of libraries contain other libraries for sensors and actuators as well.

Accelerometer

The library for the accelerometer by Grove is ” DigitalAccelerometer_MMA7660FC “. You can add it to Arduino by renaming it ” MMA7660FC ” and placing it to the library folder in Arduino folder.

#include <Wire.h>
#include <MMA7660.h>
MMA7660 accelemeter;
 
void setup(){
 accelemeter.init(); 
 Serial.begin(9600);
}
 
void loop(){
 float ax,ay,az;
 
 accelemeter.getAcclemeter(&ax,&ay,&az);
 
 Serial.print("x = ");
 Serial.println(ax); 
 Serial.print("y = ");
 Serial.println(ay); 
 Serial.print("z = ");
 Serial.println(az);
 
 Serial.println("--");
 delay(200);
}

Ultra sonic ranger finder

The library for the Ultra sonic range finder by Grove is ” Ultrasonic “. You can add it to Arduino by placing it to the library folder in Arduino folder.

#include <NewSoftSerial.h>
#include <Ultrasonic.h>

Ultrasonic ultrasonic(7);

void setup(){
  Serial.begin(9600);
}

void loop(){
  ultrasonic.MeasureInCentimeters();
  delay(150);
  Serial.print("The distance is ");
  Serial.print(ultrasonic.RangeInCentimeters);
  Serial.println(" cm");
}