Fablab Sorbonne - Paris, France

home

Characterising Programming Languages :: Embedded Programming

Python, C

Experiment with Raspberry Pi 3

First, we have to download the operating system on https://www.raspberrypi.org/downloads/.
We choose Raspbian (raspbian-2018-03-14/2018-03-13-raspbian-stretch.zip ).
Then, we unzip and have a .img file.
scanning and printing

We started the group assignment by installing the Rasbian operating system on our Raspberry Pi. We did this by using downloading the OS image from the raspberrypi.org and flashing it to a SD card using the Etcher program.

scanning and printing scanning and printing

When this is complete, you can insert the mini-SD card (housed within the SD) into the Raspberry Pi and attached the necessary peripherals (mouse, keyboard and HDMI display). It loads up with a desktop just like any computer, running Linux.

scanning and printing

We knew we had to test the use of GPIO and we searched online for the necessary libraries. We then realised that GPIO was included natively in this version of Linux (?).

scanning and printing

We chose to work with Python first. We ran the following code to get the Terminal line to return a basic printline command.

scanning and printing

We then wanted to to see if we could get the Raspberry to get an LED to blink . We assembled two jumper cables, a resistor and breadboard and attached them to GND and 5v pins on the Raspberry. We then typed the following code,

import RPi.GPIO as GPIO
import time
 
led = 14 ## led is on GPIO 14
 
GPIO.setwarnings(False) ## disable warnings
GPIO.setmode(GPIO.BCM) ## use gpio numbers and not pin numbers
 
GPIO.setup(led,GPIO.OUT) ## set the the led as an output
 
while True:
GPIO.output(led,GPIO.HIGH) ## turn led on
time.sleep(0.2) ## wait 0.2 seconds
GPIO.output(led,GPIO.LOW) ## turn led off
time.sleep(0.2)

scanning and printing

We decided to take the experiment a stage further, so to get the Raspberry to switch between blinks when a button is pressed. We added a press button to the breadboard. The following Python code was run,

import RPi.GPIO as GPIO
import time
 
led = 14 ## led is on GPIO 14
switch = 15 ## switch button is on GPIO 15
 
GPIO.setwarnings(False) ## disable warnings
GPIO.setmode(GPIO.BCM) ## use gpio numbers and not pin numbers
 
GPIO.setup(led,GPIO.OUT) ## set the the led as an output
GPIO.setup(switch,GPIO.IN) ## set the the switch button as an input
 
while True:
if GPIO.input(switch): ## if switch is pressed
for i in range(3): ## do it 3 times
GPIO.output(led,GPIO.HIGH) ## turn led on
time.sleep(0.1) ## wait 0.1 seconds
GPIO.output(led,GPIO.LOW) ## turn led off
time.sleep(0.1)

scanning and printing

In a final step, we repeated our experiment in a second programing language, C. We prepared the following code for the blink

#include <stdio.h>
#include <wiringPi.h>
 
int main(void) {
int led = 15; //GPIO.BCM 14
wiringPiSetupGpio(); //Setup the GPio in BCM numbers
 
pinMode(led,OUTPUT); //ledPin is an output
 
for (;;) { //infinite loop
digitalWrite(led,HIGH); //turn the led on
delay(500); // wait 500 milliseconds
digitalWrite(led,LOW); //turn the led off
delay(500);
}
 
return 0;
}

And the following code for the switch

#include <stdio.h>
#include <wiringPi.h>
 
int main(void) {
int led = 14; //led is on GPIO.BCM 14
int button = 15; //button button is on GPIO BCM 15
int value = 0;
wiringPiSetupGpio(); //Setup the GPio in BCM numbers
pinMode(led,OUTPUT); //ledPin is an output
pinMode(button,INPUT);
while (1) { //infinite loop
value = digitalRead(button);
if (value==HIGH) {
for (int i=0;i<3;i++) {
digitalWrite(led,HIGH);
delay(100);
digitalWrite(led,LOW);
delay(100);
}
}
}
return 0;
}