Using a Sharp GP2Y0A51SK0F IR Distance Sensor with Arduino (2cm to 15cm)
In this tutorial, we learn how to use a Sharp IR GP2Y0A51SK0F distance meter to measure distances from 2 cm to 15 cm. Code is provided for multiple sensors.
341-Arduino code for Sharp GP2Y0A51SK0F distance sensor
语言: C++
/*
*
*
This is Arduino code to measure the distance using Sharp Infrared Distance
sensor GP2Y0A51SK0F to measure from 2 to 14 cm.
See datasheet:
https://global.sharp/products/device/lineup/data/pdf/datasheet/gp2y0a51sk_e.pdf
*
* Watch video instructions for this code: https://youtu.be/rdNCIyL6OA8
*
* Written by Ahmad Shamshiri on July 09, 2020 in Ajax, Ontario, Canada
* www.robojax.com
*
* Get this code and other Arduino codes from Robojax.com.
Learn Arduino step by step in a structured course with all material, wiring diagrams, and libraries
all in one place. Purchase my course on Udemy.com: http://robojax.com/L/?id=62
If you found this tutorial helpful, please support me so I can continue creating
content like this. You can support me on Patreon: http://robojax.com/L/?id=63
or make a donation using PayPal: http://robojax.com/L/?id=64
Related videos:
Introduction to MAX6675 K-Type: https://youtu.be/VGqONmUinqA
Using MAX6675 K-Type thermocouple with LED display: https://youtu.be/cD5oOu4N_AE
Using MAX6675 K-Type thermocouple with LCD1602-I2C: https://youtu.be/BlhpktgPdKs
Using 2 or more MAX6675 K-Type thermocouples: https://youtu.be/c90NszbNG8c
Using MAX6675 K-Type thermocouple as heater or cooler controller: [this video]
* * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.*
* This code has been downloaded from Robojax.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
//33.9 + -69.5x + 62.3x^2 + -25.4x^3 + 3.83x^4
const int sensorPin[] = {A0};
float distance[1];//watch video for details https://youtu.be/rdNCIyL6OA8
const int AVERAGE_OF =50;
const float MCU_VOLTAGE = 5.0;
void setup()
{
//Robojax.com code for sharp IR sensor
Serial.begin(9600);
Serial.println("Robojax Sharp GP2Y0A51SK0F demo");
//Robojax.com code for sharp IR sensor
}//setup ends here
void loop(){
//Robojax.com code for sharp IR sensor
readDistance(0);//read sensor 1
// readDistance(1);//read sensor 2
if(distance[0] >8.5)
{
//if distance is greater than 8.5 do something here
}
// print out the value you read:
Serial.print("d1 =");//
Serial.print(distance[0]);
Serial.println("cm");
delay(300);
//Robojax.com code for sharp IR sensor
}//loop end
/*
* readDistance()
* @brief reads the distance from the sharp sensor
* and updates the appropriate sensor's value
* @param "sensor" is an integer
* @return none
* Written by Ahmad Shamshiri for robojax.com
* on July 09, 2020 in Ajax, Ontario, Canada
*/
void readDistance(int sensor)
{
//Robojax.com code for sharp IR sensor
float voltage_temp_average=0;
for(int i=0; i < AVERAGE_OF; i++)
{
int sensorValue = analogRead(sensorPin[sensor] );
delay(1);
voltage_temp_average +=sensorValue * (MCU_VOLTAGE / 1023.0);
}
voltage_temp_average /= AVERAGE_OF;
// equation of the fitting curve
////33.9 + -69.5x + 62.3x^2 + -25.4x^3 + 3.83x^4
distance[sensor] = 33.9 + -69.5*(voltage_temp_average) + 62.3*pow(voltage_temp_average,2) + -25.4*pow(voltage_temp_average,3) + 3.83*pow(voltage_temp_average,4);
//Robojax.com code for sharp IR sensor
}//readDistance
资源与参考
尚无可用资源。
文件📁
没有可用的文件。