Проблемы с подключением УЗ дальномера к Arduino Uno
- Войдите на сайт для отправки комментариев
Добрый день форумчане. Еть у меня ультразвуковой дальномер Y401(http://dvrobot.ru/238/411.html). К нему на этом же сайте прилагался скетч с примером кода. Также имеется Arduino Uno. Проблема вот в чём:
В архиве "Ультразвуковой датчик 2.0 (ID358).rar"(можно скачать с сайта приведённого выше) имется три примера кода:
1) US100_GPIO_Arduino
unsigned int EchoPin = 2; // connect Pin 2(Arduino digital io) to Echo/RX at US-100
unsigned int TrigPin = 3; // connect Pin 3(Arduino digital io) to Trig/TX at US-100
unsigned long Time_Echo_us = 0;
unsigned long Len_mm = 0;
void setup()
{ //Initialize
Serial.begin(9600); //Serial: output result to Serial monitor
pinMode(EchoPin, INPUT); //Set EchoPin as input, to receive measure result from US-100
pinMode(TrigPin, OUTPUT); //Set TrigPin as output, used to send high pusle to trig measurement (>10us)
Serial.println("OK");
}
void loop()
{
digitalWrite(TrigPin, HIGH); //begin to send a high pulse, then US-100 begin to measure the distance
delayMicroseconds(50); //set this high pulse width as 50us (>10us)
digitalWrite(TrigPin, LOW); //end this high pulse
Time_Echo_us = pulseIn(EchoPin, HIGH); //calculate the pulse width at EchoPin,
if((Time_Echo_us < 60000) && (Time_Echo_us > 1)) //a valid pulse width should be between (1, 60000).
{
Len_mm = (Time_Echo_us*34/100)/2; //calculate the distance by pulse width, Len_mm = (Time_Echo_us * 0.34mm/us) / 2 (mm)
Serial.print("Present Distance is: "); //output result to Serial monitor
Serial.print(Len_mm, DEC); //output result to Serial monitor
Serial.println("mm"); //output result to Serial monitor
}
delay(1000); //take a measurement every second (1000ms)
}
При мониторинге порта ничего не происходит
2) US100_UART_Arduino
unsigned int HighLen = 0;
unsigned int LowLen = 0;
unsigned int Len_mm = 0;
void setup()
{ //connect RX (Pin 0 of Arduino digital IO) to Echo/Rx (US-100), TX (Pin 1 of Arduino digital IO) to Trig/Tx (US-100)
Serial.begin(9600); //set baudrate as 9600bps.
}
void loop()
{
Serial.flush(); // clear receive buffer of serial port
Serial.write(0X55); // trig US-100 begin to measure the distance
delay(500); //delay 500ms to wait result
if(Serial.available() >= 2) //when receive 2 bytes
{
HighLen = Serial.read(); //High byte of distance
LowLen = Serial.read(); //Low byte of distance
Len_mm = HighLen*256 + LowLen; //Calculate the distance
if((Len_mm > 1) && (Len_mm < 10000)) //normal distance should between 1mm and 10000mm (1mm, 10m)
{
Serial.print("Present Length is: "); //output the result to serial monitor
Serial.print(Len_mm, DEC); //output the result to serial monitor
Serial.println("mm"); //output the result to serial monitor
}
}
delay(500); //wait 500ms
}
При мониторинге порта происходит следующее:


Большую часть времени отображается символ "U", при это показания расстояния похожи на реальные. В чём может быть проблемма? Заранее спасибо.
P.S: На дальномере VCC - подключёт к 5V, Tx и Rx подключены как написсано в скетче, GND(второй выход) - подключён к GND на плате. Перемычку пробовал снимать, результат тот же.
В архиве "Ультразвуковой датчик 2.0 (ID358).rar"(можно скачать с сайта приведённого выше) имется три примера кода:
1) US100_GPIO_Arduino
Большую часть времени отображается символ "U", при это показания расстояния похожи на реальные. В чём может быть проблемма? Заранее спасибо.
P.S: На дальномере VCC - подключёт к 5V, Tx и Rx подключены как написсано в скетче, GND(второй выход) - подключён к GND на плате. Перемычку пробовал снимать, результат тот же.
Найдите код символа U в таблице и посмотрите что в своем коде в строке 12 вы отправляете в монитор и датчик.
Подключил к Serial1 Arduino Mega по UART (выводы 18 и 19). Все работает.
unsigned int HighLen = 0; unsigned int LowLen = 0; unsigned int Len_mm = 0; void setup() { //connect RX (RX1 19) to Echo/Rx (US-100), TX (RX1 18) to Trig/Tx (US-100) Serial.begin(9600); //set baudrate as 9600bps. Serial1.begin(9600); } void loop() { Serial1.flush(); // clear receive buffer of serial port Serial1.write(0X55); // trig US-100 begin to measure the distance delay(250); //delay 500ms to wait result if(Serial1.available() >= 2) //when receive 2 bytes { HighLen = Serial1.read(); //High byte of distance LowLen = Serial1.read(); //Low byte of distance Len_mm = HighLen*256 + LowLen; //Calculate the distance if((Len_mm > 1) && (Len_mm < 10000)) //normal distance should between 1mm and 10000mm (1mm, 10m) { Serial.print("Distance is: "); //output the result to serial monitor Serial.print(Len_mm, DEC); //output the result to serial monitor Serial.println(" mm"); //output the result to serial monitor } } delay(250); //wait 250ms }