Raspberry Pi 002: Pi Camera, start up scripts and remote desktop

I have been on holidays, but it has been a few weeks now since I received a few new gadgets… and it’s time to write about it. (Remember the previous post about Raspberry Pi?)

What did I get? Raspberry Pi Camera!


I order it through Adafruit industries.


Which has many nice gadgets at great prices. And many tutorials, like… 3D Printed BMO with LED Matrix Face.


Hehehehe nice.

Going back to Pi camera… on this tutorial we will try

  • Take screen shots.
  • Running scripts on start up.
  • Remote desktop connection through xrdp.
  • Installing the Raspberry Pi Camera.
  • Installing PiCamera library on Python.

Remember that this is for Raspbian O.S.

1 Take screen shots.

In order to take screenshots we need to install the utility scrot. To do that, open a LXTerminal and type:

sudo apt-get install scrot

To use it after 10 seconds delay:

sudo scrot -d10

and the capture will be saved in a file with the numerical date and time with .png extension.

To display a list of Scrot comands:

sudo scrot -h


(It seems I already had it).

2 Running scripts on start up.

One of the most useful things to do with the Pi is running scripts automatically on start up. That means that when the Pi starts, it will run whatever script you write, allowing you to program many tasks.

The first thing to notice is that your Pi can start on graphic mode (GUI), or in the command shell (Non GUI), and there is things that can only be done in the GUI (e.g. like plotting a graph) and others that can be done in the Non GUI (e.g. change keyboard language).


First we create a simple Python script that plots the sine() graph from 0 to pi. It is going to be called graf.py.

from math import pi
import numpy as np
import matplotlib.pyplot as plt
x=[i / 100.*2*pi for i in range(100)]


Then we need to edit the autostart file. This file has all the commands that are going to be run after the desktop is loaded.

sudo nano /etc/xdg/lxsession/LXDE/autostart

Once the editor is openned, we just add this line to the code:

@/usr/bin/python /home/pi/graf.py


In the next start up the script will run automatically and will show the sine plot. 2013-11-01-081026_1824x984_scrot

This can be done for any Python script you want, and gives you lots of option to automatize things.


First we create a simple Python script that sums the first 100 integers and writes the result to a file. It is going to be called writenumber.py. The main thing to take into account now is that we need to specify the full path to files everywhere we use them, or the script will not work.

for b in range(100):

with open('/home/pi/test','w') as f:

If we try it, it will create a file called test.txt which has the total inside.


Once the script is working, we go to their file properties and enable “Make the file executable”. This will allow the command shell to run it.

Now to call this script on the start up, before the GUI interface appears (or if we work completely on shell mode without graphical interface), we write on a LXTerminal:

sudo nano /etc/rc.local

And on the editor we add the line

sleep 60; sudo  /usr/bin/python  /home/pi/writenumber.py


We save the file and restart… (remember to erase the previous made test.txt file)…. and voilà!


The script is executed on the start up and a new test.txt file is being created.

3 Remote desktop connection through xrdp.

The complete post on how to do it is here. But the steps are very easy.

Open a LXTerminal and install xrdp

sudo apt-get install xrdp

and with a windows xp computer, simply go to the remote desktop ant type the IP of the Pi (e.g. you can run the Pi with a screen once and see the IP).

Captura de pantalla 2014-03-11 22.24.40
The xrdp login windows appear.


Here it is the Pi with the WiFi on it and power… and the remote desktop from my laptop.


4 Installing the Raspberry Pi Camera.

And now, at last, for the Pi Camera. It is really easy to install it. Just open the Raspberry and plug the wire.


In one position will not make any contact at all, so don’t worry. (In the video they have the regular Pi, mine was mod with heat-sinks).

Remember to pass the wire through the case before connecting it.


Ok, so now it is connected. Is it ready to go? Not yet. We need to enable it. For that, we just follow the tutorial on the official webpage. which I copy here.

Start the Pi and open a LXTerminal to update.

sudo apt-get update
sudo apt-get upgrade

Acces the configuration settings and enable the camera.

sudo raspi-config

and navigate to “camera” and select “enable”


Then finish and reboot.

Now to take a simple image… open a LXTerminal and run

raspistill -o image.jpg

It will be saved on the main folder.


To see the rest of commands (including streaming), visit the official webpage.

5 Installing PiCamera library on Python.

Ok ok, now for fun. Pi camera and Python, the picamera module!

But before, remember the previous posts on Python:

maths with Python

maths with Python 2: Rössler system

maths with Python 3: Diffusion Equation

maths with Python 4: Loading data

maths with Python 5: Double Compound Pendulum Chaotic Map

Specially the first Raspberry Pi post where I introduce Python and matplotlib for Raspberry Pi.

Ok, so, we want to make the camera work alone and being able to see remotely what is being recorded. How to do it?

First, get the libraries for using the camera on Python (thanks to Dave Jones for them).

apt-get update

apt-get install python3-picamera


apt-get install python3-picamera

Once it is installed, we only need to go to Python and use some of the picamera codes. You can find the documentation here.

For this post, I will use the script for saving pictures every 10 seconds. Basically, this scripts initialices the camera, adjust the resolution, the rotation, and take some pictures.

import picamera
from time import sleep

camera = picamera.PiCamera()
for i in range(4) :

And voila!

For now is quite a lot. I will come back with the Picamera later in another projects, but for now I think this is quite useful.


5 thoughts on “Raspberry Pi 002: Pi Camera, start up scripts and remote desktop”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s