Change Raspberry Pi Hostname. By default the Raspberry Pi has its hostname set to “raspberrypi”. This appears as part of the command prompt when you open a terminal window. You can change it by editing a few system files. This process only takes a minute. Open a terminal window from your program list. Type : sudo leafpad /etc/hostname followed by ENTER. Then type : sudo leafpad /etc/hosts followed by ENTER. Then you can either reboot by typing : sudo reboot followed by ENTER or simply restart the hostname process by typing : sudo /etc/init.d/hostname.sh start followed by ENTER.
The hostname as been changed. Please ensure you choose a hostname that only contains upper and lower case letters, numbers and the “-” character. Alternative Editor In the steps above we used Leafpad to edit the two system files. Sudo nano /etc/hostname sudo nano /etc/hosts The advantage with using Nano is that it can be run from the command line and does not require a graphical interface. Change Raspberry Pi Hostname. Install RPi.GPIO Python Library. The RPi.GPIO Python library allows you to easily configure and read-write the input/output pins on the Pi’s GPIO header within a Python script.
Thankfully this library is now including in the standard Raspbian image available from the Foundations Download Page. If you are using a fresh image you don’t need to install it but I’ve kept the instructions here in case you ever want to try a manually installation. Method 1 – Install from repository If the package exists in the Raspbian repository is can be installed using apt-get. First you need to update the available package versions : sudo apt-get update Then attempt to install the RPi.GPIO package : sudo apt-get install rpi.gpio If it isn’t already installed it will be installed.
Method 2 – Manual Installation The package is available from and the current version is 0.5.11 (February 2015). Step 1 – Download the library wget Example Usage. Ntrol LED Using GPIO Output Pin. The circuit below shows to turn an LED on and off using a Raspberry Pi GPIO pin configured as an output. It uses the output pin to turn on a transistor which allows the LED to draw current from the 5V supply. The following header pins are required : Header Pin 2 : 5VHeader Pin 6 : GroundHeader Pin 11 : GPIO The Header Pins are defined in my Raspberry Pi Header Pin page. The LED is a standard 5mm red type with a forward voltage of 2V.The transistor could be a BC547, BC548 or equivalent.Resistor R1 limits current through the LED from the 5V pin.
R1 could be increased you just need to make sure you allow enough current to power your LED. R2 could be increased and this would reduce the current drawn from the GPIO pin but it would reduce the maximum current allowed to flow through the transistor. Note : As with all connections you make to the Pi’s PCB you must double check everything. I’ve modified some of the details on this page thanks to feedback from Gert (see comments below). Python Code. Raspberry Pi Speakers & Analog Sound Test. I decided to attach some speakers to my Raspberry Pi. I didn’t want to spend much so I picked up a pair of stereo speakers from my local Poundworld. They cost £1 exactly. They aren’t exactly the sort of speakers you would hook up to your HiFi system but for experimenting you can’t beat the value. They have a 3.5mm jack which plugs into the Pi’s analog audio connector. In time I want to play sound files from my Python programs to provide audio notifications. Install the Sound Drivers Update : The newer Debian “Wheezy” image enables the sound driver by default so this section can be ignored.
In the Debian “Squeeze” image the ALSA sound drivers are considered experimental and have been disabled by default. Sudo apt-get install alsa-utils That will install the required sound driver files and tools. Sudo modprobe snd_bcm2835 By default the output is set to automatically select the interface (HDMI if available otherwise analog).
Sudo amixer cset numid=3 <n> sudo amixer cset numid=3 1. Reading Analogue Sensors With One GPIO Pin. Unlike some other devices the Raspberry Pi does not have any analogue inputs. All 17 of its GPIO pins are digital. They can output high and low levels or read high and low levels. This is great for sensors that provide a digital input to the Pi but not so great if you want to use a sensor that doesn’t. For sensors that act as a variable resistor such as LDRs (Light Dependent Resistors) or thermistors (temperature sensors) there is a simple solution. It allows you to measure a number of levels using a single GPIO pin. It uses a basic “RC” charging circuit (Wikipedia Article) which is often used as an introduction to electronics.
T = RC where t is time, R is resistance (ohms) and C is capacitance (farads) So the trick is to time how long it takes a point in the circuit the reach a voltage that is great enough to register as a “High” on a GPIO pin. With a 10Kohm resistor and a 1uF capacitor t is equal to 10 milliseconds. Here is the circuit : Here is the circuit implemented on a breadboard :
Getting Your Raspberry Pi Serial Number Using Python. Every Raspberry Pi has a unique serial number. It is sometimes useful to extract this number to identify the hardware that your Python scripts are running on. The following function “getserial()” can be used to return a string containing the unique serial number. In reality this is the serial number of the Broadcom CPU but given you are unlikely to remove the CPU from the PCB it was supplied on this can be considered the serial of the whole device. Here is the Python function : def getserial(): # Extract serial from cpuinfo file cpuserial = "0000000000000000" try: f = open('/proc/cpuinfo','r') for line in f: if line[0:6]=='Serial': cpuserial = line[10:26] f.close() except: cpuserial = "ERROR000000000" return cpuserial If you include this definition at the beginning of your Python script you can use it to set a variable equal to the serial number : myserial = getserial()
Raspberry Pi P5 Header. The Raspberry Pi Revision 2.0 introduced a new header to the PCB. This header is called P5 and consists of 8 pins. It is located directly underneath the standard 26 way P1 GPIO header. It was intended that this header is populated on the underside of the PCB so any connectors or cables do not interfere with the P1 header. Pin 1 has a square pad whereas the other 7 have circular pads. The P5 pins provide : +5V, 3V3 and GroundGPIO28GPIO29GPIO30GPIO31 Here are some photos showing the location of the P5 header in relation to the 26 way P1 header : P5 Header (Topside) P5 Header (Underside) Here is the pin-out of the P5 header where the square pad is always Pin 1 and Pin 8 is always in the opposite corner : +5V+3V3GPIO28GPIO29GPIO30GPIO31GroundGround When viewing the underside of the board the layout is mirrored but Pin 1 is still marked with a square pad and Pin 8 is always in the opposite corner.
This header is unpopulated to use it you need to solder your own pin header to the board. How to change the Raspberry Pi password. The default Raspbian SD card image configures a default user account called ‘pi’ with a password of ‘raspberry’. Every device using this image will therefore have the same username and password combination. This is great for quickly getting starting but isn’t very good practice from a security point of view.
You should therefore consider changing the default password as soon as possible. It’s easy to do and only takes a minute. Here are the simple steps you need to follow : From a command line prompt type passwd followed by the Enter key.Enter the current password followed by the Enter key.Enter the new password followed by the Enter key.Re-enter the new password followed by the Enter key.Your password has now been changed. Your screen will look something like the screenshot below: Changing your default password is important if your Raspberry Pi is connected to the Internet. . … oh just make sure you don’t forget the new password! Here is a video showing the process : Macro Photography With The Raspberry Pi Camera Module. In a recent article I mentioned how you can add cheap interchangeable lenses to your Raspberry Pi camera to make it more versatile.
I’ve now played around with the macro lens and thought I’d show you a set of photos I took. My son is really interested in insects so I decided to take some macro photos of whatever I could find behind the sofa. The lucky subjects were a moth and some sort of fly which we found in the conservatory (someone had vacuumed under the sofa!). The Fly The fly was my first attempt. The focal point is about an inch in front of the lens and I held the fly with tweezers as I moved it in and out of focus. Holding the tweezers still wasn’t easy.
The Moth My second attempt was a moth. You can see its furry body and coiled proboscis. The results are quite cool and demonstrate just what you can achieve with a bit of experimentation. For children this is an amazing opportunity to learn. Running A Python Script At Boot Using Cron. There maybe times when you want to run a Python script when your Raspberry Pi boots up. There are a number of different techniques to do this but I prefer the method that uses “cron”. Cron is a job scheduler that allows the system to perform tasks at defined times or intervals. It is a very powerful tool and useful in lots of situations. You can use it to run commands or in this case, a Python script. Step 1 – Create A Python Script The first step is creating your Python script. This will be the script that will run at boot time. Double check you’ve got the correct path by typing : cat /home/pi/MyScript.py This should show the contents of your script.
Make sure your script works and does what you expect it to. Step 2 – Add A New Cron Job To create a new job to Cron we will modify the “crontab”. Sudo crontab -e Each user of the system (ie “pi”) can have its own Crontab but in this case we want to add it as an admin so we prefix our “crontab -e” command with “sudo”. Sudo reboot sudo kill 1863. PiPan Pan And Tilt Mechanism For Raspberry Pi Camera. Analogue Sensors On The Raspberry Pi Using An MCP3008. The Raspberry Pi has no built in analogue inputs which means it is a bit of a pain to use many of the available sensors.
I wanted to update my garage security system with the ability to use more sensors so I decided to investigate an easy and cheap way to do it. The MCP3008 was the answer. The MCP3008 is a 10bit 8-channel Analogue-to-digital converter (ADC). It is cheap, easy to connect and doesn’t require any additional components. It uses the SPI bus protocol which is supported by the Pi’s GPIO header. This article explains how to use an MCP3008 device to provide 8 analogue inputs which you can use with a range of sensors.
In the example circuit below I use my MCP3008 to read a temperature and light sensor. Here are the bits I used : Raspberry PiMCP3008 8 channel ADCLight dependent resistor (LDR)TMP36 temperature sensor10 Kohm resistor The first step is enabling the SPI interface on the Pi which is usually disabled by default. Circuit The CH0-CH7 pins are the 8 analogue inputs. Quick Guide To nano Text Editor On The Raspberry Pi. If I need to edit text files directly on my Raspberry Pi my text editor of choice is nano. There are other text editors available but I prefer nano’s relatively straightforward interface. As a command line based utility it may feel strange for users who are more familiar with a graphical interface but it is easy to learn the basics.
Syntax colouring is available which makes reading and reviewing scripts easy. Launching nano To start nano you can simply type nano at the command prompt. This will launch with a “new buffer”. In order to launch an existing text file you type nano followed by the file name : nano berryclip_01.py You will see something like this : nano Main Screen If your file is not in the current directory you will need to specify the full path : nano /home/pi/berrryclip/berryclip_01.py If the file is a system file you may need to use elevated permissions : sudo nano -w /etc/fstab Note: Always use the -w switch when opening system files.
Editing Text nano Help Pages (CTRL+G) Ultrasonic Distance Measurement Using Python – Part 1. LEDs, buzzers and switches are the most common items people attempt to interface to their Raspberry Pi’s. Something I found in eBay that is a little bit different is an ultrasonic measurement module. This allows you to measure the distance to the nearest wall or solid object. The modules are easy to buy, cheap and relatively straight forward to interface to the GPIO header. So here is some information on my experiments with an Ultrasonic measurement module and Python.
In future projects I can see these modules being a great way to add some intelligence to a Pi powered robot or car. The HC-SR04 module cost approximately £3 ($5) and is the size of a box of matches. Connecting To The Pi Powering the module is easy. The input pin on the module is called the “trigger” as it is used to trigger the sending of the ultrasonic pulse. You can use any GPIO pins you like on your RPi but you will need to note the references and amend your Python script accordingly. Ultrasonic Module Circuit Python Script. Cheap PIR Sensors and the Raspberry Pi – Part 1. A great little sensor you can add to your Raspberry Pi projects is a PIR module. These 5V “Passive Infra Red” sensors are available for a few pounds from eBay. They can be powered from 5V and output 3V so can be connected directly to pins on the Pi’s GPIO header without any other components.
The module sets a single output pin high whenever it detects movement within its field of view. It holds this pin High (3.3V) for a minimum period of time. I am currently using one in an alarm system and it works great for such a small and cheap device. PIR Connections Here is a diagram showing the pin-out on the PIR module and how I connected it to my Raspberry Pi : PIR Module The device has two variable resistors that you can adjust to tweak the performance of the module. Trimming Controls The first one (left-hand side on the photo) determines the sensitivity of the device. The units available on eBay vary in specification but they are all very similar. Python Example Script This can then be run using :
Gertboard – The Ultimate Raspberry Pi Add-on Board. The Gertboard is an add-on board designed by Gert van Loo who is also one of the hardware engineers involved in the original design of the Raspberry Pi. The board was originally sold by Farnell/Element14 in kit form but has just been re-launched as a fully assembled unit. This may come as a disappoint to those people that wanted to solder their own boards but perhaps a relief to those that will be happy with a pre-built product. The Gertboard plugs directly onto the Raspberry Pi GPIO header and provides a range of features and interfacing possibilities including : Drive powerful motorsDetect switch pressesIlluminate LEDsDrive relays.Detect and output analogue voltagesRespond to external physical events Specifications & Features 12 x LED indicators12 x buffered I/O3 x momentary push buttons6 open collector drivers (50V, 0.5A)18V, 2A bi-directional motor controller28-pin dual in line ATmega microcontroller2-channel 8 bit Digital to Analogue converter Gertboard – Layout Diagram Photos User Manual.
Ultrasonic Distance Measurement Using Python – Part 2. Cheap PIR Sensors and the Raspberry Pi – Part 2. Raspberry Pi 1- Wire Digital Thermometer Sensor. Top 10 Things to Connect to Your Raspberry Pi. Berryclip 6 LED Board Testing in C. Raspberry Pi CPU Usage Monitoring With A BerryClip. The Official Raspberry Pi Camera Module. Pi Camera Module Mechanical Dimensions. How To Disable The Red LED On The Pi Camera Module. Installing The Raspberry Pi Camera Module. Taking Hi-Res Photos With The Pi Camera Module. Capturing HD Video With The Pi Camera Module. Creating Timelapse Videos With The Raspberry Pi Camera.
Adding A Watermark Or Logo To A Timelapse Video Using Avconv. Raspberry Pi Command Line Audio.