Monthly Archives: March 2014

If you need to backup or make a copy of Raspberry Pi SD Card then there are some tools available. However, this post explain how to use command line on Mac OS X in creating an image file (backup file) of an SD Card and later this image file can be used to put into another SD Card.

Now compare the results of running command df -h before and after inserting the SD card and look for the new device. In my case it is /dev/disk1s1. This is the device name of the partition.

After finding the device name of the partition now we are going to determine the raw device name of the entire disk by omitting the final “s1” and replacing “disk” with “rdisk” (this is very important: you will lose all data on the hard drive on your computer if you get the wrong device name). Make sure the device name is the name of the whole SD card as described above, not just a partition of it, for example, rdisk1, not rdisk1s1. In my case it is /dev/rdisk1

Run the following command to create an image file from the SD Card

sudo dd if=/dev/rdisk1 of=/path/to/filename.img bs=1m

The above command is simply taking the whole disk (SD card) and create an image file to the location “/path/to/filename.img”. You may use any filename. Note that dd command will not feedback anything to the terminal unless there is an error or it is finished. While waiting for the dd command to finish you may see the progress by pressing Control+t

In order to create an SD card from the image file (any image file or the one you just created), determine the device name of the partition of the SD card using df -h as described above. In my case it is /dev/disk1s1. Then unmount the partition so that you will be allowed to overwrite the disk:

My operator requires to put APN but no username and password required. Sakis3g will guide you how to enter APN but if you do not put username it will quit and cannot connect. What I do is I enter a blank space for my username and password then it connects.

One may want to connect an ultrasonic sensor with a Raspberry Pi. In fact, there many posts talks about this already but I have it here for my own record and in fact most of the content of this post inspired by this site.

I have got HC-SR04 ultrasonic sensor which is a fairly cheap sensor in hand. An ultrasonic sensor can be used to measure the distance between the object itself and an object in front of it by sending out an ultrasonic pulse and listening for its echo (a reply or response). In practice one could connect this sensor with other devices other than Raspberry Pi such as Arduino.

One may use any other GPIO pins. In this case GPIO 17 will be an output from Raspberry Pi to the sensor’s Trig pin. This pin is used to trigger the sensor to send out an ultrasonic signal. According to the datasheet a signal of 10 microseconds is a required to trigger the sensor.One may use voltage divider to lower down 5V to 3.3V instead.

In addition, a 1K Ohm resister is required to drop down the output of the Echo pin to 3.3V since Raspberry Pi GPIO pins support 3.3V. Hence, the GPIO 27 used in this case acts as an input pin from the sensor to the Pi.

#!/usr/bin/python
# remember to change the GPIO values below to match your sensors
# GPIO output = the pin that's connected to "Trig" on the sensor
# GPIO input = the pin that's connected to "Echo" on the sensor
def reading(sensor):
import time
import RPi.GPIO as GPIO
# Disable any warning message such as GPIO pins in use
GPIO.setwarnings(False)
# use the values of the GPIO pins, and not the actual pin number
# so if you connect to GPIO 25 which is on pin number 22, the
# reference in this code is 25, which is the number of the GPIO
# port and not the number of the physical pin
GPIO.setmode(GPIO.BCM)
if sensor == 0:
# point the software to the GPIO pins the sensor is using
# change these values to the pins you are using
# GPIO output = the pin that's connected to "Trig" on the sensor
# GPIO input = the pin that's connected to "Echo" on the sensor
GPIO.setup(17,GPIO.OUT)
GPIO.setup(27,GPIO.IN)
GPIO.output(17, GPIO.LOW)
# found that the sensor can crash if there isn't a delay here
# no idea why. If you have odd crashing issues, increase delay
time.sleep(0.3)
# sensor manual says a pulse ength of 10Us will trigger the
# sensor to transmit 8 cycles of ultrasonic burst at 40kHz and
# wait for the reflected ultrasonic burst to be received
# to get a pulse length of 10Us we need to start the pulse, then
# wait for 10 microseconds, then stop the pulse. This will
# result in the pulse length being 10Us.
# start the pulse on the GPIO pin
# change this value to the pin you are using
# GPIO output = the pin that's connected to "Trig" on the sensor
GPIO.output(17, True)
# wait 10 micro seconds (this is 0.00001 seconds) so the pulse
# length is 10Us as the sensor expects
time.sleep(0.00001)
# stop the pulse after the time above has passed
# change this value to the pin you are using
# GPIO output = the pin that's connected to "Trig" on the sensor
GPIO.output(17, False)
# listen to the input pin. 0 means nothing is happening. Once a
# signal is received the value will be 1 so the while loop
# stops and has the last recorded time the signal was 0
# change this value to the pin you are using
# GPIO input = the pin that's connected to "Echo" on the sensor
while GPIO.input(27) == 0:
signaloff = time.time()
# listen to the input pin. Once a signal is received, record the
# time the signal came through
# change this value to the pin you are using
# GPIO input = the pin that's connected to "Echo" on the sensor
while GPIO.input(27) == 1:
signalon = time.time()
# work out the difference in the two recorded times above to
# calculate the distance of an object in front of the sensor
timepassed = signalon - signaloff
# we now have our distance but it's not in a useful unit of
# measurement. So now we convert this distance into centimetres
distance = timepassed * 17000
# return the distance of an object in front of the sensor in cm
return distance
# we're no longer using the GPIO, so tell software we're done
GPIO.cleanup()
else:
print "Incorrect usonic() function varible."
print reading(0)

The above code is saved with file name ‘usonic.py’. Ensure that this code is executable.

chmod +x usonic.py

The code must be run with sudo (super user) since it requires accessing hardware.

Somehow I want to start firefox on my Ubuntu 12.04 LTS from command line in fullscreen mode. Firefox can be invoked from command line using command:

firefox

However, the command does not seem to have fullscreen option. This site uses xdotool to simulate that button F11 on the keyboard is pressed so the firefox goes on fullscreen. Hence, I a simple bash script helps achieving this task. Here is the script:

firefox http://localhost/abc.html & # one can use any url
xdotool key F11 # hit full screen

I save the above script as “firefoxfullscreen.sh” and I run it whenever I need it. Note that the script is relatively simple and it should be modified based on your purposes. One possibility is to add delay in between the two line as shown below: