Tutorial de como utilizar Ultrasom com Arduino

O Ultrasom é um sensor que pode ser utilizado como medidor de distância, detecção de objetos e entre outros.

Neste tutorial, mostraremos como utilizar o Ultrasom da Maxbotix com Arduino.

Este Ultrasom pode ser usado por comunicação Serial (RS-232, portanto não conecte diretamente no Arduino senão irá queimá-lo), por tensão analógica e por comprimento de pulso (PWM). Aqui mostraremos apenas o uso por tensão analógica e por comprimento de pulso(PWM). A menor distância que o ultrasom consegue detectar é de 20cm e a maior distância é de 6,5m.

O Ultrasom da Maxbotix tem seis furos, um GND, um 5V, um TX, um RX, um AN, um PW e um BW. Aqui só usaremos o GND, 5V, AN e PW. 

Para utilizá-lo por tensão analógica, conecte o GND no GND do Arduino, o 5V no 5V do Arduino e o AN no pino A0 do Arduino.

A programação está demonstrada abaixo:


//Feel free to use this code.
//Please be respectful by acknowledging the author in the code if you use or modify it.
//Author: Bruce Allen
//Date: 23/07/09
//Analog pin 0 for reading in the analog voltage from the MaxSonar device.
//This variable is a constant because the pin will not change throughout execution of this code.
const int anPin = 0;

//variables needed to store values
long anVolt, inches, cm;
int sum=0;//Create sum variable so it can be averaged
int avgrange=60;//Quantity of values to average (sample size)

void setup() {
//This opens up a serial connection to shoot the results back to the PC console
Serial.begin(9600);
}

void loop() {

pinMode(anPin, INPUT);

//MaxSonar Analog reads are known to be very sensitive. See the Arduino forum for more information.

//A simple fix is to average out a sample of n readings to get a more consistant reading.\\
//Even with averaging I still find it to be less accurate than the pw method.\\
//This loop gets 60 reads and averages them

for(int i = 0; i < avgrange ; i++) {

//Used to read in the analog voltage output that is being sent by the MaxSonar device. //Scale factor is (Vcc/512) per inch. A 5V supply yields ~9.8mV/in
anVolt = analogRead(anPin);
sum += anVolt;
delay(10);

}


inches = sum/avgrange; cm = inches * 2.54;
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();

//reset sample total
sum = 0;

delay(500);

}

Agora, abra o Serial Monitor e mostrará os valores em polegadas (in) e em centimêtros(cm).

Agora, demonstraremos o funcionamento por PWM. A vantagem de utilizar por PWM é que este é mais preciso do que por tensão analógica.
conecte o GND no GND do Arduino, o 5V no 5V do Arduino e o PW no pino 7 do Arduino.
A programação está demonstrada abaixo:

//Feel free to use this code.
//Please be respectful by acknowledging the author in the code if you use or modify it.
//Author: Bruce Allen
//Date: 23/07/09
//Digital pin 7 for reading in the pulse width from the MaxSonar device.
//This variable is a constant because the pin will not change throughout execution of this code.
const int pwPin = 7;
//variables needed to store values
long pulse, inches, cm;

void setup() {

//This opens up a serial connection to shoot the results back to the PC console
Serial.begin(9600);
}

void loop() {

pinMode(pwPin, INPUT);

//Used to read in the pulse that is being sent by the MaxSonar device.

//Pulse Width representation with a scale factor of 147 uS per Inch.

pulse = pulseIn(pwPin, HIGH); //147uS per inch
inches = pulse/147; //change inches to centimetres
cm = inches * 2.54;

Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();

delay(500);

}

Agora, abra a Serial Monitor e mostrará os valores em polegadas(in) e em centimêtros (cm).

E pronto! Agora conseguimos medir distância com um arduino e um ultrasom! Até a próxima!

Referências:

http://www.arduino.cc/playground/Main/MaxSonar

http://labdegarag1.lojatemporaria.com/arduino-original/arduino-uno....

http://labdegarag1.lojatemporaria.com/sensores/sensor-de-distancia-...

Exibições: 11736

Comentar

Você precisa ser um membro de Laboratorio de Garagem (arduino, eletrônica, robotica, hacking) para adicionar comentários!

Entrar em Laboratorio de Garagem (arduino, eletrônica, robotica, hacking)

Comentário de Everson Santana em 25 julho 2013 às 14:35

Boa tarde!

Se alguem puder me ajudar, preciso intalar um sensor de torque em um painel didatico se alguem tiver alguma idéia algum tutorial. Será de grande ajuda 

Comentário de Guilherme Fiusa em 4 maio 2013 às 12:00

caso eu queira usar o mesmo sensor mas so que com o garagino tem como ?

© 2024   Criado por Marcelo Rodrigues.   Ativado por

Badges  |  Relatar um incidente  |  Termos de serviço