Добрый день!
Приобрёл УЗ дальномер Y401 (http://dvrobot.ru/238/411.html). По инструкции подключил его к Arduino Uno и залил в него пример кода предложенный на том же сайте магазина.
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)
}
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)
}
При мониторинге порта на Arduinо, тишина. Окно пустое.
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" и редко расстояние.(скриншот мониторинга прилагаю)
Подключение проверял.
Подскажите в чём может быть проблема?