久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 15671|回復: 2
收起左側

arduino之arduino uno與超聲波傳感器的使用

[復制鏈接]
ID:60076 發表于 2014-4-20 23:32 | 顯示全部樓層 |閱讀模式
    今天開始玩單片機,選的是Arduino。因為arduino作為單片機入門級別的,使用起來很方便。

     今天做的是加了超聲波傳感器。

     一般來說,市面上的超聲波傳感器分為兩種:一種是trigger型;一種是尋址發指令型。(這是我自己的定義,如果有錯請勿見怪啊。

    trigger型比較簡單,就是通過一個IO口作為Input,通過高低電壓驅動這個trigger,超聲波開始工作。然后再用pulseIn(EchoPin, HIGH)計算往返時間,再用公式計算距離。這種方法比較單一,簡單,這樣的超聲波價格較低,精度還不錯。可以說是物美價廉。下面貼上代碼:

const int TrigPin = 2; //用arduino的數字口2定義為Trigpin,用于輸入觸發
const int EchoPin = 3; //用arduino的數字口3定義為EchoPin,用于返回時間
float cm; //距離
void setup()
{
Serial.begin(9600); //波特率,一般為9600,這里的是串口的,這里用串口實際上是為了用到串口監視器看得到的數據
pinMode(TrigPin, OUTPUT);
pinMode(EchoPin, INPUT);
}
void loop()
{
digitalWrite(TrigPin, LOW); //低高低電平發一個短時間脈沖去TrigPin
delayMicroseconds(2);
digitalWrite(TrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin, LOW);

cm = pulseIn(EchoPin, HIGH) / 58.0; //將回波時間換算成cm
cm = (int(cm * 100.0)) / 100.0; //保留兩位小數
Serial.print(cm); //用串口監視器。在arduino IDE的tool的serial monitor那里可以看到監視器。上面

//是send,發送數據,下面是數據print的內容(這是串口發送數據出去的內容,這里就用這個來看收到的數

//據)。rx的串口的接收端,tx是發送端。
Serial.print("cm");
Serial.println();
delay(1000);
}

    具體連線:將超聲波trigger端和arduino的數字接口2相連,EchoPin端和數字接口3相連,VCC連5v,gnd連地。接著燒入代碼,就可以檢測到距離了。

   



    另一種是尋址發指令類型的:

    這里要用到TTL串口(RX和TX)或者I2C(wire型,總線類型?梢耘獛资畟超聲波)。

    在這里我就舉TTL串口的例子吧!使用的超聲波是KS103

    超聲波工作原理是主機(就是Uno了)發送指令給超聲波,首先是發送超聲波的地址,找到地址。如KS103的地址是0xe8,但是uno只能用7位地址的話,就要右移一位了。接著發送指令0x02,這是寄存器,返回的數據就存在里面。然后是指令超聲波的工作模式,具體看超聲波的文檔。下面是代碼實現:

#define KS103 0xe8 // default address of KS103 is 0xe8
#define reduce_noise 0x71 // these lines define the data for commands. See data sheat.
#define turn_off_led 0xc1
#define read_distance 0xb0 // distance may be less than 5m .
#define read_brightness 0xa0
#define read_temperature 0xc9
word reading=0; // thease variables are for printing the temperture through Serial.print().
void setup()
{
Serial.begin(9600); // start Serial transaction

delaydelayMicroseconds(90);

Serial.print(KS103);

delaydelayMicroseconds(90);

Serial.print(0x02);

delaydelayMicroseconds(90);

Serial.print(reduce_noise); // send the noise reduction command to KS103
delay(2000); // wait for being stabilized 2seconds
}

void loop()
{

Serial.flush();//清空串口緩存
Serial.print(KS103); // measure the distance
delaydelayMicroseconds(90);

Serial.print(0x02);

delaydelayMicroseconds(90);

Serial.print(read_distance);

if(2 <= Serial.available()) // wait the register available
{
reading = Serial.read(); // read register 2
reading = reading << 8; // shift the data read to upper byte
reading |= Serial.read(); // read the register 2 to lower byte
//數據一般都是16位的,要兩次read才可以都會數據

}

delay(250); //wait to be read on the screen of PC.
}

    連線如下:將超聲波RX與主機TX相連,TX與主機RX相連。VCC和GND就不用說了。

這樣就完成了TTL串口的超聲波。當然,這里只用到了測距,沒有用到溫度傳感器和亮度傳感器。這種超聲波傳感器是比較高級的,這些傳感器上面也都有。

接下來我說一下I2C的超聲波使用:限于篇幅,我就簡單地貼貼代碼:

#include
#define KS103 0xe8 // default address of KS103 is 0xe8
#define reduce_noise 0x71 // these lines define the data for commands. See data sheat.
#define turn_off_led 0xc1
#define read_distance 0xb0 // distance may be less than 5m .
#define read_brightness 0xa0
#define read_temperature 0xc9
word reading=0; // thease variables are for printing the temperture through Serial.print().
word reading2=0;
void setup()
{
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
Wire.begin(); // join the TWI as the master
Serial.begin(9600); // start Serial transaction
//Wire.begin(KS103);
KS103_command(reduce_noise); // send the noise reduction command to KS103
delay(2000); // wait for being stabilized 2seconds
KS103_command(turn_off_led); // turn off the LED
}

void loop()
{
digitalWrite(6,LOW);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
digitalWrite(7,HIGH);
Serial.println(KS103_read_command(read_distance)); // measure the distance
//Serial.print(" ");
reading = KS103_read_command(read_temperature); // measure the temerature
reading2 = reading & 0x0f;
reading = reading >> 4; // shift low byte 4 bits
Serial.print(reading);
//Serial.print('.');
Serial.print(reading2);

Serial.print("deg.");
Serial.print(" brightness ");
Serial.print(KS103_read_command(read_brightness)); // measure light
Serial.println(".");
delay(250); //wait to be read on the screen of PC.
}


String reading_temperature(){ // read the temperature data
word reading = KS103_read_command(read_temperature);
word reading2 = reading & 0x0f;
reading = reading >> 4; // shift low byte 4 bits
return (String)reading + (String)reading2;
}

word KS103_read_command(byte command){ // sending the command and read the data in the register
word reading =0;
// step 1: instruct sensor to read echoes
KS103_command(command); // send the command
// step 2: wait for readings to happen
delay(10); // wait 1 milliseconds // I'm not sure if this line is neccesary. I mimic the sketch for other TWI devices.
// step 3: instruct sensor to return a particular echo reading
Wire.beginTransmission(KS103); // start to transmit with KS103
Wire.write(byte(0x02)); // register 2 is the gate of receiving the data
Wire.endTransmission(); // stop transmitting
// step 4: request reading from sensor
Wire.requestFrom(KS103, 2); // request the data in the 2nd and thir register of KS103
// step 5: receive reading from sensor
if(2 <= Wire.available()) // wait the register available
{
reading = Wire.read(); // read register 2
reading = reading << 8; // shift the data read to upper byte
reading |= Wire.read(); // read the register 3 to lower byte
}
return reading; // return the 16bit data
}

void KS103_command(byte command){ // send the command to KS103
Wire.beginTransmission(KS103); // start the transmission with KS103
Wire.write(byte(0x02)); // register 2 Wire.write(byte(0x02));
Wire.write(command); // send the command to the register 2
Wire.endTransmission(); // end of transmission
delay(30);//added by chenyu
}
回復

使用道具 舉報

ID:63074 發表于 2014-6-18 17:42 | 顯示全部樓層
好貼怎么沒人頂。
回復

使用道具 舉報

ID:64894 發表于 2014-8-9 15:57 | 顯示全部樓層
不錯,KS103超聲波還是不錯的
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 中文字幕精品一区久久久久 | 中文字幕在线精品 | 91在线精品视频 | 最近中文字幕在线视频1 | 国产精品亚洲一区二区三区在线 | 亚洲福利视频网 | 亚洲综合色 | 亚洲国产精品第一区二区 | www.夜夜骑.com | 亚洲天堂免费在线 | 免费在线性爱视频 | 久久亚洲一区 | 成人一区二区三区在线观看 | 亚洲免费在线观看av | 天堂中文在线观看 | 91精品国产91久久久久游泳池 | 久久伊人一区 | 日韩高清成人 | 欧美日韩在线观看一区 | 日本亚洲欧美 | 天天综合永久入口 | 亚洲国产成人精品久久久国产成人一区 | 你懂的在线视频播放 | 精品国产一区二区在线 | 欧美成人激情视频 | 精品粉嫩aⅴ一区二区三区四区 | 国产精久久久久久 | 欧美一区二区三区在线 | 国产视频久久 | 国产精品96久久久久久 | 久久久精品一区二区三区 | 亚洲精品一二三区 | 国产九九九九 | 国产精品一区二区免费 | 亚洲日本欧美日韩高观看 | 美女天天操 | 国产精品不卡 | 久久毛片 | 亚洲人成人一区二区在线观看 | 国产伦一区二区三区久久 | 91精品中文字幕一区二区三区 |