Tuesday, August 20, 2013

Ultrasonic distance measurement using HC-SR04 ultrasonic module + Arduino Pro

Ultrasonic sensors (Ping sensors) are widely used in distance measurement as they are cheap and has a quite good accuracy. I got this cheap HC-SR04 ultrasonic module from ebay and wanted to test it.
I found the datasheet for it from here.

According to the datasheet ;
Max Range = 400cm
Min Range = 2cm

This algorithm should be used to get the echo from the module:
1. Give 10uS pulse to the TRIGGER pin.
2. Measure the pulse width at ECHO pin.
3. Calculate  distance = (echo high level time×velocity of sound) / 2

I connected the module to my Arduino Pro (Techduino) and wrote a small code for the testing. The code will send the calculated distance value to PC via its UART port.

























The code :

#define echo 8
#define trigger 9
long duration,distance;

void setup() {
 Serial.begin (9600);
 pinMode(trigger, OUTPUT);
 pinMode(echo, INPUT);
 digitalWrite(trigger, LOW);
}

void loop() {
 digitalWrite(trigger, LOW);
 digitalWrite(trigger, HIGH);
 delayMicroseconds(10);
 digitalWrite(trigger, LOW);
 duration = pulseIn(echo, HIGH);
 distance = duration/58;
 if ((distance >= 400) || (distance <= 2))
 distance = -1;
 Serial.println(distance);
 delay(60);
 
}
I checked the output from the Arduino to PC using a terminal program.


















After that I coded a small software to get the distance data and show it in a nice way using Visual C#. This is my first C# application and I referred here and here for coding. You can download the source code and application from here.




Thursday, August 8, 2013

Computer controlled automated guided vehicles (AGV) using Parellel port interfacing

As we built a relay board to interface with "parallel port" in my earlier post. Today I'm going to talk about the control software for it. I wrote this software long time ago using Visual Basic 6 and inpout32.dll as the interface. I used this to control some modified toy cars as autonomous vehicles similar to automated guided vehicles (AGV). The path can be programmed to the software in realtime or in advance. So, the vehicle will follow the route as we need. You can download the program with source code from here.

The software interface:













This was first designed as a school science exhibition project in 2007 and recently used to control a mars rover model in a local astronomy exhibition at Sri Chandananda Buddhist College:













The mars rover model:

















The control circuit:

RS-485 Communication testing with MAX485

As we know about RS-232 communication protocol, I'm going to try RS-485 communication between two nodes. RS-485 can support multiple slaves and mostly used in industrial applications with high separation distance between nodes. I recently got 10pcs of MAX485 IC from ebay for $1 which is a RS-485 transceiver. I also got  USB to RS-485 convertor based on CH430 + MAX485. I'm using another USB-UART convertor based on PL2303 for this test setup. So, I have two USB-UART chips connected using two MAX485 ICs in between,which provides the RS-485 communication.

My test setup according to MAX485 datasheet.
(Image extracted from datasheet)











As MAX485 is half duplex, I have to use one as TX with following setup. I'm using my USB-RS485 adapter as  RX and there is no need of manual setting as RX.











My TX setup on breadboard:

















My RX setup, the one is right is USB-RS485 convertor:
















Test results :