We used Ultrasonic Sensor HC-SR04 for the purpose of measuring the level of the Fuel
Hardware requirement
- HC-SR04
- 1k Ohm Resistor
- 1.5k Ohm Resistor
- Jumper wires
- BreadBoard
Specification of HC-SR04:
Quiescent Current : <2mA
Working Current: 15mA
Effectual Angle: <15°
Ranging Distance : 2cm – 400 cm/1? – 13ft
Resolution : 0.3 cm
Measuring Angle: 30 degree
Trigger Input Pulse wi
Dimension: 45mm x 20mm x 15mm
Pins :
- VCC: +5VDC
- Trig : Trigger (INPUT :Sends pulses to object)
- Echo: Echo (OUTPUT :output is of 5v)
- GND: GND
Voltage Divider:
The ECHO output is of 5v. The input pin of Raspberry Pi GPIO is rated at 3.3v. So 5v cannot be directly given to the unprotected 3.3v input pin. Therefore we use a voltage divider circuit using appropriate resistors(always R1=R and R2<=2R) to bring down the voltage to 3.3V.
Circuit Diagram:
Screenshot of the Hardware connections:
Output :
Python Code:
We used the Python script to interact with the Hardware as there is a limitation in Java while interacting with these kind of sensors(because of waiting issue).
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM) #GPIO_mode=BCM=refers pin by broadcom SOC channel number
GPIO_TRIGGER=14 #assign TRIGGER output pin num=14
GPIO_ECHO=15#assign ECHO I/P pin num=15
GPIO.setup(GPIO_TRIGGER,GPIO.OUT)
GPIO.setup(GPIO_ECHO,GPIO.IN)
GPIO.output(GPIO_TRIGGER,False)
time.sleep(0.5) # waits for 0.5s
GPIO.output(GPIO_TRIGGER,True)
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER,False)
start = time.time()
while GPIO.input(GPIO_ECHO)==0:
start = time.time()
while GPIO.input(GPIO_ECHO)==1:
stop = time.time()
elapsed=stop-start
distance=elapsed*34000
distance=distance/2
print "Distance: %.1f"%distance
GPIO.cleanup()
Java Code:
As we are publishing the sensor data to the cloud using Kura, We will
use the Java code to execute and get output of the python script.
Also we used ScheduledExecutorService to schedule the publishing for every 30 seconds interval
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class CallPython {
static ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
public static void main(final String[] args) {
try {
scheduledExecutorService.schedule(new Runnable() {
@Override
public void run() {
final Runtime r = Runtime.getRuntime();
Process p = null;
try {
p = r.exec("sudo python DistanceMeasure.py");
} catch (final IOException e) {
e.printStackTrace();
}
final BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
try {
while ((line = br.readLine()) != null) {
if (line.contains("Distance:")) {
System.out.println("Distance: " + line);
}
}
} catch (NumberFormatException | IOException e) {
e.printStackTrace();
}
}
},
30,
TimeUnit.SECONDS);
} catch (final Exception e) {
final String cause = e.getMessage();
if (cause.equals("python: not found")) {
System.out.println("No python interpreter found.");
}
}
}
} .
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class CallPython {
static ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
public static void main(final String[] args) {
try {
scheduledExecutorService.schedule(new Runnable() {
@Override
public void run() {
final Runtime r = Runtime.getRuntime();
Process p = null;
try {
p = r.exec("sudo python DistanceMeasure.py");
} catch (final IOException e) {
e.printStackTrace();
}
final BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
try {
while ((line = br.readLine()) != null) {
if (line.contains("Distance:")) {
System.out.println("Distance: " + line);
}
}
} catch (NumberFormatException | IOException e) {
e.printStackTrace();
}
}
},
30,
TimeUnit.SECONDS);
} catch (final Exception e) {
final String cause = e.getMessage();
if (cause.equals("python: not found")) {
System.out.println("No python interpreter found.");
}
}
}
} .