Experiment: use an arduino as a slave to your raspberry pi

IMG_20141219_180749.jpg

 

Why do this?

If you have got past the initial excitement of having a Raspberry pi, and tried getting into experimenting with inputs and outputs (i/o for short), you might have stumbled across the fact that the Raspberry Pi only has the ability to read digitally from it’s GPIO (general purpose input/output) pins.

IMG_20141219_175420.jpg

So if you have a variable resistor (potentiometer) or a light dependent resistor (or any other analogue sensor) – you won’t be able to measure anything but 1 or 0 (that is how digital works, you are either HIGH or LOW, as opposed to analogue which can be anything in between). Now there are some cool ways around this if you are into your electronics (see here or here). But there are a couple of good reasons for using an Arduino as ‘slave’ to read analogue inputs.

IMG_20141219_180655.jpg

Firstly, the Arduino is very reliable and safe – it has been designed for tinkering. You can make a few mistakes (within reason) when plugging in wires and choosing resistors. The Pi on the other hand is a big wus. Plug a 5v wire into one of its GPIO digital pins (designed for 3.3 volts) and you are likely to fry it! The other big reason to slave the Arduino – is that you can get the full power of Python (including libraries like PyBrain for Neural Networks) hooked up to the outside world through the Arduino’s analogue capabilities.

IMG_20141219_182802.jpg

Yes, using the Arduino IDE (integrated development environment aka a program that makes it easy to write Arduino software) is good and more than enough to keep anyone going even on very complex projects. But using BOTH the Pi and Arduino has it’s advantages IMHO. You get the power of the Pi and Python (think camera, image processing, audio etc) as well as the analogue, safe, reliability of the arduino. (some will be thinking it might be easier to just get a breadboard and a 10 bit ADC, and you might be right, but I would argue that, once set up, this is easier for those new to electronics).

[all experiments are conducted at your own risk]

 

Step 1: Install the Arduino IDE

Make sure you have an internet connection. Open up LXTerminal and type in:

$ sudo apt-get update

This updates the Raspberry Pi with an up to date list of software that is available.

Then type in the following:

$ sudo apt-get -y install arduino

This installs the Arduino IDE on your Pi using the program ‘apt-get’. The -y just means say yes to any questions during the installation.

You might be wondering why we have to install the Arduino IDE after all the fuss about using Python to connect with your Arduino. The reason is – we can use it to install Sketches/firmware on the Arduino, which is really handy for trouble shooting (more on this later).

 

Step 2: Install nanpy (a Python library) using setup tools 

nanpy will help us write the Python code which will help us set up the Arduino as slave to the Pi! AKA we will be able to read and write from the Arduino’s analogue pins.

$ wget http://bootstrap.pypa.io/ez_setup.py - O - | sudo python

(Make sure to use a capital O, not a zero, or you will get errors.)

 

Step 3: Install pyserial

nanpy needs to be able to talk with the Arduino using what is called a ‘serial connection’. To do this we need to install pyserial.

First download the pyserial tar.gz file using wget. 

$ wget http://pypi.python.org/packages/source/p/pyserial/pyserial-2.7.tar.gz

Now use tar to ‘unzip’ it:

$ tar zxf pyserial-2.7.tar.gz

(hint: if you press tab after starting to type ‘pyserial-2.7.tar.gz’ it will autocomplete the rest – which means no more typos! If you get nothing, try pressing tab twice)

Next move into the newly ‘unziped’ directory:

$ cd pyserial-2.7 (again tab works a treat here)

We can now use python to install the pyserial library:

~/pyserial-2.7 $ sudo python setup.py install

Step 4: Install nanpy 0.8

Move back to the home directory if you haven’t already:

~/ cd ~

Then download the nanpy 0.8 tar.gz:

$ wget https://pypi.python.org/packages/source/n/nanpy/nanpy-v0.8.tar.gz

Like we did in step 3, just go ahead and unzip it and install it.

$ tar zxf nanpy-v0.8.tar.gz

$ cd nanpy-0.8

~/nanpy-0.8 $ sudo python setup.py install

Step 5: This step is not entirely needed (see step 1) but is a good way to check everything is okay with the Pi → Arduino usb connection.

 Open the Arduino IDE from the Pi menu:

IMG_20141219_182856.jpg

Menu → Programming → Arduino IDE

Plug in the Arduino to a powered usb hub – not directly into your pi. This makes sure the Pi gets enough current to work properly.

Click Tools → Serial Port and select ‘/dev/ttyACM0’

This tells the Arduino IDE where to connect, in this case ‘/dev/ttyACM0’. 

Click open, which opens a menu. Chose 01. Basics → Blink

This opens a ‘sketch’ which will blink on and off the orange LED built onto the Arduino (pin 13).

Click upload to compile the sketch and load it onto your Arduino.

If everything is okay, you should see a blinking light on your Arduino?

 

Step 6: Upload a special firmware used to work with nanpy v0.8

Your Arduino will need you to load a special sketch on it (firmware) for it to work with nanpy. To do this, you will need to make it first.

Move from the nanpy-0.8 directory into the firmware directory using cd:

~/nanpy-0.8 $ cd firmware

Then type in the following:

~/nanpy-0.8/firmware $ export BOARD=uno

~/nanpy-0.8/firmware $ make

(you will get lots of output)

~/nanpy-0.8/firmware $ make upload

(you should see upload progress displayed)

 

Step 7: Run a simple python script to see if it is working 

Now to test out your first Python AND Arduino experiment!

First we will write a simple python script using the text editor nano.

$ nano blink.py

When nano opens, type in the following Python code:

''' import from nanpy and time modules '''
from nanpy import Arduino
from nanpy import serial_manager
from time import sleep

''' create a connection to the Arduino '''
serial_manager.connect('/dev/ttyACM0')

''' set pin 3 as an output '''
LED = 13
Arduino.pinMode(LED, Arduino.OUTPUT)

''' set the blink rate '''
delay = 1

print "starting"

''' while true aka start an infinite loop of led blinking '''
while True:
    Arduino.digitalWrite(LED, Arduino.HIGH)
    print delay
    sleep(delay)
    Arduino.digitalWrite(LED, Arduino.LOW)
    sleep(delay)

Press control and x to exit the file.

Type y to save changes.

Then type:

$ python blink.py

and watch the led on your Arduino blink. When you are ready, press control c to end the script.

 

Sources

Special thanks go to the following for inspiring and informing this post:

themagpi.com

Tony Goodhew and Raspberry Pi forum community

nanpy V0.8

Tim Greenwood

Tim currently works for Living Systems. His background is in psychology, and he has been working as a psychologist within education and the community for the last ten years. He recently gained a Masters of Arts in Management, Learning, and Leadership from Lancaster Business school focusing on the use of Action Research to promote curious and evolving ways of working with individuals, groups and organisations.

More Posts - Website

Khaya Centre Mount Olive 2014: visit+

IMG_20140723_073402.jpg

  [previous post | next post]

Other things we achieved during the visit…

Codecademy.com

2014-07-23

 We managed to get www.codecademy.com working (after installing gnash) which was really exciting.

Loom Bands

IMG_20140722_132342.jpg IMG_20140723_132411.jpg IMG_20140723_132434.jpg

Yes, loom bands have made it to Lehae! Nyah taught one girl how to do a loom band bracelet and by the last day, it had gone viral. And one of the trustees was very happy when she got her turn to learn too. She also saw the educational benefit of loom bands for the Khaya Centre: to strengthen hand and arm muscles for learning to write and hand eye coordination (we’re glad someone has found a benefit to loom bands).

IMG_20140723_103900.jpg

Rainbow FM

Yes we did – we got on the radio in Jo’burg to talk about Urban Shift’s painting project and Project Pi.

IMG_20140724_053640.jpg IMG_20140724_065212.jpg IMG_20140724_064103.jpg IMG_20140724_065706.jpg

Tim Greenwood

Tim currently works for Living Systems. His background is in psychology, and he has been working as a psychologist within education and the community for the last ten years. He recently gained a Masters of Arts in Management, Learning, and Leadership from Lancaster Business school focusing on the use of Action Research to promote curious and evolving ways of working with individuals, groups and organisations.

More Posts - Website

Khaya Centre Mount Olive 2014: Visit

IMG_20140718_092345.jpg

 [previous post | next post]

Visit: July 2014

2014-07-14

Even with Tyson’s meticulous planning (yes Tyson is the master planner when it comes to details) , we just had to hope we had done enough. There were just so many unknowns. Who would I be working with in Lehae? What was the electricity like? Would there be Wifi? Would there be an electrical store near by if I needed any extra supplies?

Getting on the plane was exciting, I just had to stop thinking about it all and relax.IMG_20140715_135919.jpg Well that was until our flight was delayed and we had to spend 24 hours in Amsterdam. This was in some ways a mixed blessing – we got chance to get to know each other as a team which we had not had much chance to do before setting off.

2014-07-17IMG_20140716_111347.jpg

Arrival – Nelson Mandela Day

Our first day of work was amazingly on Nelson Mandela day.

nelson-mandela-day.jpg We had the opportunity to help with volunteers from businesses in Johannesburg who had come to help paint houses and plant little mini vegetable gardens for some of the families accessing the Khaya Centre.

IMG_20140719_160930.jpg IMG_20140718_142904.jpg IMG_20140718_112149.jpg

Setting Up

2014-07-23

Mandla, project lead South Africa side, set straight to setting up the Pi’s. I just took the pictures. And as we talked, I found out he has a background in business management and was taking time out of his studies to support the good work of the Khaya Centre.

IMG_20140719_080731.jpgIMG_20140719_081520.jpgIMG_20140719_082505.jpgIMG_20140719_082704.jpg2014-07-19IMG_20140719_083249.jpgIMG_20140719_085026.jpgIMG_20140719_111249.jpgIMG_20140719_120654.jpgIMG_20140719_112214.jpgIMG_20140719_120648.jpg2014-07-192014-07-192014-07-19IMG_20140722_155417.jpg

Once the hardware was all good. I set on checking the Pi each had a working operating system, Libre Office installed and setting up Wifi (via a wireless router which connects to the internet via 3G).

Training

The first step was to figure out what people already knew about Linux and computing in general, and how confident they felt learning about what they didn’t know (and how much time they could give to this from their other duties). We also looked at what we knew about Libre Office, Scratch and Minecraft (which for Nyah, aged 10, was a lot more than the rest of us put together). We then looked at spending some time on what I called ‘deep emersion’ or in other words – having a play with a Pi for a while.

 

IMG_20140721_131610.jpgIMG_20140721_172329.jpg2014-07-23IMG_20140721_131703.jpg

We decided to start the project with four groups. Two groups from the primary aged children and two groups of ‘seniors’. We planned out a basic introductory lesson, based upon some teaching from the front but also using Nyah (aged 10) to do a bit demonstrating, as well as time for unstructured exploration with some adult facilitation as needed. A big inspiration for the project and the style of delivery, was to try and get away from more formal modes fo instruction, and encourage the children and young people to learn by doing, and showing each other as they learnt. A key influence here was Sugata Mitra’s work.

 

Running the first sessions

The first impressions seemed to be unbeleif, then followed by excitement. This was the first time some of the younger children had used a computer, so coordinating the mouse and keyboard took a bit of time. But even within the first session we were seeing children teech each other how to move around in Minecraft and create sprites in Scratch (seniors).

2014-07-232014-07-23DSC_0425.JPGDSC_0423.JPG

 

Governance

I was shmoosing at a lunch one evening, kindly put on by one of the Khaya Centre trustees. I got talking with a retured high school principal. We were talking about Project Piu and the benefits. He seemed to see the educational benefits and we soon got talking about managing such a project and ensuring long term impact and quality. OFSTED came up, and I aksed him if he wouldn’t mind playing a kind of OFSTED role for the project. Perhaps with less fo the inspection feel and more of the supportive and guidance/leadership vibe. Within half an hour we had set a date for him to visit the project (the last day of the trip) and meet with Mandla (project lead). The meeting was very productive and we agreed a plan for the next 6 months.

UPDATE: I have spoken with Valencia who tells me that Mandla has also been introduced to an ICT teacher who works at a nearby school.

 

Tim Greenwood

Tim currently works for Living Systems. His background is in psychology, and he has been working as a psychologist within education and the community for the last ten years. He recently gained a Masters of Arts in Management, Learning, and Leadership from Lancaster Business school focusing on the use of Action Research to promote curious and evolving ways of working with individuals, groups and organisations.

More Posts - Website

Khaya Centre Mount Olive 2014: Planning

IMG_20140412_102323.jpg

[previous post | next post]

Planning

20130729_172123.jpg

So who knew having a good ideas is just the tip of the ice burg when it comes to pulling off a quality project? Luckily for Project Pi – I had teamed up with Moira and Esther (from Urban Shift) who were already planning to take out a group from Bolton to the Khaya Centre, in July 14. They were taking four teenagers and one adult – to help with a project to help improve the quality of living for 15 households in Lehae project. They had been out before as part of the building of the centre and wanted to follow up with some targeted outreach to families accessing the Khaya Centre. It made a lot of sense to partner up with this venture and go out as a group. We arranged with Valencia to sleep within the centre and she agreed to coordinate and organise our overnight security, transport and daily food and refreshments which was fantastic.

IMG_20140718_112426.jpg

Architecture

Tyson used his background in cloud architecture to design the Raspberry Pi set ups. This included deciding specifically what to buy, from the Raspberry Pi all the way down to the SD cards, mice, cables and monitors. Each had to be sourced, checked for availability, priced up and  checked for suitability. For example, the power supplies for the Raspberry Pi had to supply enough current for the Pi’s which were overclocked and running with USB WiFi adaptors – we found out early on that the Pi would just cut out and restart if they did not have enough current available. Tyson was meticulous with side of the project preparation, aiming to keep the number of surprises in South Africa to a minimum. We didn’t know how easy it would be to source things like SD cards, cables or monitors when in Lehae so we decided to take it all out with us.

Software

During the planning phase we tested out a number of free software packages, most of which now come as standard with Noobs (New Out Of the Box Software), a handy bundle of operating system software supplied by the Raspberry Pi foundation. These were Scratch (a visual programming language designed for kids), Minecraft Pi and Python (Minecraft Pi comes with an API that allows kids to write Python code that can interact with the Minecraft world).

We also decided to install Libre Office, Chromeum and gnash. Libre Office is a free office suite which Valencia thought would be very useful for teaching basic word processing and spreadsheet skills as well as writing CVs. Chromeum with gnash (an open source version of Flash) was also installed to allow access to Codecademy.com, an excellent website for learning to code Python (and many more languages).

Teaching and Learning

Right from the start, I had been really keen for this project to be more than just an ICT suite. I wanted the way kids were taught and encouraged to interact with the Pi to be ‘child led’. I had been really impacted by a number of key people:

  • Seymour Papert – I had experienced first hand as a child the thrill of making a Logo turtle/robot move by typing in simple lines of code. Having to persevere and iterate to make it move in the way we had imagined.
  • Sugata Mitra – and his experiments with putting high speed internet into rural villages in India, leaving children to explore and find for themselves answers to their questions, spontaneously teaching peer to peer.

As well as the materials developed by Craig Richardson (http://arghbox.wordpress.com/) – a curriculum for learning Python using Minecraft Pi.

Tim Greenwood

Tim currently works for Living Systems. His background is in psychology, and he has been working as a psychologist within education and the community for the last ten years. He recently gained a Masters of Arts in Management, Learning, and Leadership from Lancaster Business school focusing on the use of Action Research to promote curious and evolving ways of working with individuals, groups and organisations.

More Posts - Website

Khaya Centre Mount Olive 2014: Background

IMG_20140718_092537.jpg

[previous post | next post]

Background to trip

IMG_20140725_070703.jpg I first met Valencia in September 2013. Valencia leads a vibrant and growing project in Johannesburg, South Africa – called The Khaya Centre Mount Olive. The project is located in Lehae, amidst an expansive housing project built to accommodate beneficiaries of Eikenhof and Thembelihle settlements (see here for more on the Khaya Centre). At the time, I was with a group called Urban Shift who had first met Valencia, whilst helping to build the Khaya Centre in 2012. As we got talking, I was struck by the passion Valencia had for her project, and the vision she had for its future.  (Hear Valencia talking about her project here)

There was a moment, where the conversation seemed to really come alive , Valencia was speaking about the limited access to IT her centre beneficiaries and members of the local community have, and I suddenly thought about Raspberry Pi. What if these small, relatively cheap and easy to maintain little computers might be suitable for her projects needs? I myself was experimenting with three of them with my three kids at home with interesting results.

FB_IMG_13845500802340365.jpg

The next day, I excitedly took a Raspberry Pi along to a charity Valencia was visiting (HAFWAY). We managed to get the Raspberry Pi hooked up to a TV and some WiFi – and after a bit of surfing the internet, playing with minecraft and looking at scratch, it seemed that she liked what she saw. We tentatively agreed to pursue the prospect of bringing a number of Raspberry Pi out to the Khaya Centre. I had no idea how many, or how we would fund this. But there seemed to be a match with some of her projects needs and the what she saw the Pi could deliver.

20131102_114016.jpg I had originally been put on to Raspberry Pi by an IT savvy friend, Tyson, who worked for an innovative IT company in Manchester, UKFast. I asked him if he wouldn’t mind spending some time with me exploring the possibilities of the Pi and in particular, if we could set up the Raspberry Pi for use in an internet cafe. At this stage I was wondering if it might be helpful for the Khaya Centre to have the ability to use the Pi to create a small revenue, adding a certain degree of ‘sustainability’ to the project. I wondered – would it be possible for members of the local community to pay a small amount to use the internet or write and email CVs? I liked the idea of the project having the ability to pay for itself over time, to have a small business enterprise feel to it – not just an ICT project. Not just setting up an IT suite. I liked the idea of involving the community and as I recalled – this had also been something Valencia had specifically mentioned during her visit to the UK. Valencia had also spoken about other projects of this kind at the centre. A sewing project that enabled local women to sell what they made and a similar project involving growing garden vegetables.

Tyson and I had set up a google+ community group where we could share ideas in between meeting up. Tyson had also added some other friends who had experience in networking and linux (the operating system used on the Raspberry Pi). This had been a good place to let the project evolve. But come March, I noticed things had somewhat stalled. I remember that went to sleep one night wondering if the project would ever come to anything. The diea seemed good, people had shown interest. There was a fit between the technology and the Khaya Centre’s needs. But one question remained. How would we fund the project?

But then I got an email. It was Tyson, letting me know that his employer, UKFast had agreed to fund 10 Raspberry Pi and all the extras (see press release here).

It was April 2014 and all of a sudden – this was it – Project Pi was on.

IMG_20140706_154922.jpg

Tim Greenwood

Tim currently works for Living Systems. His background is in psychology, and he has been working as a psychologist within education and the community for the last ten years. He recently gained a Masters of Arts in Management, Learning, and Leadership from Lancaster Business school focusing on the use of Action Research to promote curious and evolving ways of working with individuals, groups and organisations.

More Posts - Website

Valencia talking about the Khaya Centre

 

Tim Greenwood

Tim currently works for Living Systems. His background is in psychology, and he has been working as a psychologist within education and the community for the last ten years. He recently gained a Masters of Arts in Management, Learning, and Leadership from Lancaster Business school focusing on the use of Action Research to promote curious and evolving ways of working with individuals, groups and organisations.

More Posts - Website