-
Notifications
You must be signed in to change notification settings - Fork 11
GP2Y0A60SZ analog distance sensor
Sensor looks like this:
the GP2Y0A60SZ is an analog distance sensor that measures the distance from the sensor to an object. It can detect objects up to 150 cm (60 in).
- Operating voltage: 2.7 V to 5.5 V (5V version) or 2.7 V to 3.6 V (3V version)
- Average current consumption: 33 mA (typical)
- Distance measuring range: 10 cm to 150 cm (4 in to 60 in)
- Output type: analog voltage
- Update period: 16.5 ± 4 ms
- Enable pin can optionally be used to disable the emitter and save power
The sensor uses infrared light to detect how far an object is from it and converts the distance into an analog voltage that follows the graph shown below.
The distance sensor can be interfaced with nn analog-to-digital converter (ADC). An ADC must be used to convert the output analog voltage into a digital value. The TM4C has several ADC's that can convert analog voltages of up to +3.3V into a value between 0-4095 (16 bits). These ADC can be used to interface the distance sensor. The ADC, GPIO, and Distance Sensor driver/libraries can be used to easily interface the sensor.
In order to interface with the sensor follow these couple of steps:
-
Connect the sensor with the TM4C: Connect the OUT pin of sensor to a valid analog pin on the TM4C (look at the ADC library for a full list of valid analog pins). Connect the VCC to the +3.3V pin on TM4C (you can connect it to 5V but the TM4C's ADC won't be able to convert voltages above +3.3V, so it is not recommendable to do that). Connect EN (enable) pin to the +3.3V pin through a 10k ohm resistor (if you don't want to use the enable pin feature) or to a GPIO digital output pin through a 10k ohm resistor (if you want to use the enable pin feature). MAKE SURE to not forget to connect the 10k ohm resistor between both pins or you could damage the sensor and microcontroller. Finally, connect the GND pin to ground.
-
Import the necessary libraries to interface with the sensor using the "#include" macro. In this case the necessary libraries are: GPIO, ADC, DistanceSensor, and Timers.
#include <lib/GPIO/GPIO.h>
#include <lib/DistanceSensor/DistanceSensor.h>
#include <lib/ADC/ADC.h>
#include <lib/Timers/Timer.h>
int main(void){
//code...
}
- declare a variable (preferably global) of type "DistanceSensor_t"
DistanceSensor_t sensor;
int main(void){
//code...
}
- Initialize the necessary GPIO ports for the sensor. 1) an analog input pin with alternate mode on and alternate function 8 (ADC) for OUT pin 2) a digital output pin if using EN pin feature (optional).
int main(void){
/* Initialize PD2 GPIO to read analog voltage OUT pin of sensor */
GPIOConfig_t PD2Config = {PIN_D2, NONE, false, true, 8, true};
GPIOInit(PD2Config);
/* Initialize PA7 GPIO for enable pin (EN) */
GPIOConfig_t PA7Config = {PIN_A7, NONE,true, false, 0, false};
GPIOInit(PA7Config);
}
- Initialize the ADC using "ADC_init" and the distance sensor using either "DistanceSensor_ENInit" if using EN pin feature or "DistanceSensor_Init" if not.
Using DistanceSensor_ENInit:
-
first parameter: The variable of type "DistanceSensor_t" declared in step 3 passed as a pointer by simply putting a '&' in front of the variable name.
-
Second parameter: The name of the analog pin from the TM4C you are going to use for the sensor (look at the ADC library for full list of analog pins and their names).
-
third parameter: The name of the variable of type "GPIOConfig_t" for the GPIO digital input for the EN pin.
DistanceSensor_t sensor;
int main(void){
ADC_Init(); //initialize ADC
DistanceSensor_ENInit(DistanceSensor_ENInit(&sensor, PD2, PA7Config);
}
Using DistanceSensor_Init:
-
First parameter: The variable of type "DistanceSensor_t" declared in step 3 passed as a pointer by simply putting a '&' in front of the variable name.
-
Second parameter: The name of the analog pin from the TM4C you are going to use for the sensor (look at the ADC library for full list of analog pins and their names).
DistanceSensor_t sensor;
int main(void){
ADC_Init(); //initialize ADC
DistanceSensor_Init(DistanceSensor_ENInit(&sensor, PD2);
}
- Use either "DistanceSensor_SetInterrupt" or "DistanceSensor_Read" to update the data from the sensor. If you want to constantly check the data from the sensor every so often, it os highly recommended to use the "DistanceSensor_SetInterrupt" function. On the other hand, if you want to update the data at a specific time, it is recommended to use the "DistanceSensor_Read" function. the most recent data from the sensor will be stored in the "data" field of the DistanceSensor_t variable. It is accessible by: .data. i.e: if the name of the variable is "sensor" then to access the data field you would do: sensor.data.
Using DistanceSensor_SetInterrupt:
This function initializes an interrupt that will be triggered at the desired frequency and that will update the data from the sensor.
-
First parameter: The variable of type "DistanceSensor_t" declared in step 3 passed as a pointer by simply putting a '&' in front of the variable name.
-
Second parameter: the frequency at which you want the sensor data to be updated at, given in Hz. Note: in order to not lose any data from sensor, it is highly recommended that you set the frequency of the interrupt at more than twice the update frequency of the sensor (look at the "Specifications" section for the sensor's update rate). i.e: if the update frequency of the sensor is 100 Hz, you will need to call the interrupt at at least 201 Hz.
-
Third parameter: the timer that you want to use for the interrupt. You cannot use two timer at once. Look at the Timers library for a full list of all the available timers.
-
Fourth parameter: the priority of the interrupt ranging from 0-7 where 0 is the highest priority while 7 is the lowest.
DistanceSensor_t sensor;
int main(void){
/**
* setup an interrupt for distance sensor that triggers and reads the analog voltage from OUT
* at 200 Hz (every 50 microseconds) using TIMER_0A for the interrupt and setting the interrupt
* to priority 0 (highest priority).
**/
DistanceSensor_SetInterrupt(&sensor, 201, TIMER_0A, 0);
}
Using DistanceSensor_Read:
This function updates the data field for the sensor once when it is called, so it is not periodic and should be called within the main loop.
- First parameter: The variable of type "DistanceSensor_t" declared in step 3 passed as a pointer by simply putting a '&' in front of the variable name.
DistanceSensor_t sensor;
int main(void){
/* main loop */
while(1){
DistanceSensor_Read(&sensor);
}
}
- Finally, use the data from sensors to make the robot do something dependent on that data. Below is an example of how sensor data an be used and how to access it.
DistanceSensor_t sensor;
int main(void){
while(1){
if(sensor.data < 2048){
RobotDance();
}
}
}