Wednesday, September 11, 2013

Fun with Ultrasonic Ranging Device and Buzzer

This is my first Arduino project. It is a subset of the object avoidance robot project that I am planning to work over the weekends. In this project, I will make Arduino scream louder as soon as anything comes closer to the range sensor (from front, obviously).

The circuit diagram is as below:
This image is created using Fritzing
Here are the components:

  1. Ultrasonic Ranging Module HC-SR04
  2. Buzzer 2 KHz
  3. 100Ω Resistor
  4. Arduino Uno
I have connected ranging modules ground to Arduino ground pin, Vcc to 5V pin, echo to pin 7, and trigger to pin 8. Buzzer's negative (black) terminal is connected to another Arduino ground pin and positive terminal is connected to a 100Ω resistor. Which, in turn, is connected to pin 5 which is a PWM pin.

Here is the Arduino sketch that I used:

const int TRIG = 8;
const int ECHO = 7;
const int BUZZ = 5;

void setup(){
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
  pinMode(BUZZ, OUTPUT);
  Serial.begin(9600);
}

void loop(){
  long duration, inches, cm;
  
  digitalWrite(TRIG, LOW);
  delayMicroseconds(2);  
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  
  duration = pulseIn(ECHO, HIGH);
  inches = duration / 74 / 2 ;
  cm = duration / 29 / 2 ;
  
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  if(cm < 20){
    analogWrite(BUZZ, (20 - cm) * 10);
  }else{
    digitalWrite(BUZZ, LOW);
  }
  
  delay(100);
}

You can see that I setup the pins in startup() and in loop I do the distance checks. I send a LOW signal for 2 microsecs, then HIGH for next 10, then turn off. I then read from ECHO input. It tells me the time in microsec that it took to get the echo of the signal that was sent. We use speed-time-distance formula to get the distance in inches and centimeters. We print it to Serial to observe this value in Serial console of Arduino IDE.

Now that we know the distance. Here comes the logic to make sound. If the object is closer than 20 cms, I write (20 - distance) * 10. This means the sound gets louder as the object comes closer to the sensor. If the object is more than 20 cms away, the buzzer will stay mum.

Here is the video. Sorry for the bad English.


Here is the final product:


Thursday, May 23, 2013

PPTP VPN Fails Frequently?

I have upped my Ubuntu to Raring Ringtail 13.04. Everything was just awesome. I set up my workspace. Now, it was the time to code. Woohoo! Connected to PPTP VPN to checkout code.  While I was checking out the code, VPN disconnected. Then on, each time I connect to VPN, it gets disconnected in a minute or so. Never stayed long enough to do something useful. When I switched from ethernet to another broadbad wi-fi, PPTP was stable. So, there was something wrong with my ethernet setting.

After a lot of head banging and, hit and trial, I landed on this post on a forum. It was not very encouraging, plus it was really old. I gave it a try anyway and voilà! it worked! The trick is to lower MTU value. MTU is maximum transmission unit, to quote Wikipedia:
In computer networking, the maximum transmission unit (MTU) of a communications protocol of a layer is the size (in bytes) of the largest protocol data unit that the layer can pass onwards. MTU parameters usually appear in association with a communications interface (NIC, serial port, etc.). Standards (Ethernet, for example) can fix the size of an MTU; or systems (such as point-to-point serial links) may decide MTU at connect time.
 I have set MTU to 1200 down from 1500. Using sudo ifconfig eth0 mtu 1200 command in Ubuntu.
#Before
$ sudo ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:24:d8:d8:32:09
          inet addr:192.168.1.33  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: feff::224:e8ff:fe88:3209/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:142813 errors:0 dropped:0 overruns:0 frame:0
          TX packets:97325 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:174397037 (174.3 MB)  TX bytes:8177616 (8.1 MB)

#Execute command
$ sudo ifconfig eth0 mtu 1200

#After (successfully connected to VPN and stable)
$ sudo ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:24:d8:d8:32:09
          inet addr:192.168.1.33  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1200  Metric:1
          RX packets:142851 errors:0 dropped:0 overruns:0 frame:0
          TX packets:97372 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:174401881 (174.4 MB)  TX bytes:8184649 (8.1 MB)