Skip to content

9. Embedded programming#

Group Assignment: compare the performance and development workflows for other architectures

Microcomputers Testing#

The Plan: Run little programs on a number of microcomputers available at the Kamakura FabLab.

  1. The Microbit
  2. The STM32 F103
  3. The Rasperry Pi

Microbit#

(…Sasaki-san, please write a short impression of your experience with Microbit here)

STM32 F103#

We referenced this website here to understand the STM32 and upload a program to it

Technical Specifications:

  • Core architecture: ARM® 32-bit Cortex®-M3 CPU Core
  • Clock: 72 MHz maximum frequency
  • Memory: 128 KB(Flash), 20KB (SRAM)
  • 7 Timers:
    three 16bit timers, 16bit motor control PWM timer, 2 watchdog tomers, SysTick timer 24bit downcouter
  • Up to 9 communication interfaces:
    Up to 2xI2C, Up to 3 USARTs, Up to 2 SPIs, CAN interface, USB2.0
  • Power: 2.0 - 3.6V

Uploading a Program

  • We aimed to run a blink program using the STM32.
  • We set up the STM32 on a breadboard and connected it to the laptop via an FTDI cable and jumper wires.
  • We set up the STM32 F203 as a new board on Arduino to allow us to upload Arduino’s “blink” program to it. Please note that it was necessary to change the LED port number from PB1 to PC13.
  • Lots of failed attempts…the instructions on the website were very detailed and specific…and we missed a few instructions.

We realized that we had to change the jumper below from Operating mode to Programming mode.

  • After we followed the instructions exactly…we successfully uploaded “blink” to the STM32

Impression

  • A “Raw” microcontroller without a nice and easy plug-in and go ease of use of the Microbit
  • STM32 could be one of the powerful candidate when we want to do high-speed processing that is difficult with AVR.

Rasberry Pi#

Raspberry Pi is one of the most useful device for making some prototype. Also, because Raspbian is a Debian based Linux Operationg System, this small device works as a server machine. Here, we will show a sample of programming on Raspberry Pi3.

(Note) I use Raspberry Pi3 ModelB+ released 2018 June.

Python: Blinking LED

Python is one of the major programming language that is used in Raspberry Pi. It is very easy to write.

Here is the GPIO Pin number of Raspberry Pi3.

We wired Red LED as following

  • GPIO 23 -> LED (+)
  • LED(-) -> 330 Oham Register
  • Register -> GND

To control GPIO, we installed WiringPi library

1
pip install wiringpi

Here is the code to blink LED in one second interval: LED.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
import wiringpi
import time

led_pin = 23


wiringpi.wiringPiSetupGpio()

wiringpi.pinMode(led_pin, 1)

while True:

    wiringpi.digitalWrite(led_pin,1)

    time.sleep(1)

    wiringpi.digitalWrite(led_pin,0)

    time.sleep(1)



The result is here:

Nodejs: Scanning BLE devices around Pi.

Raspberry Pi has Bluetooth Low Energy modules. And, we can use it for scanning and counting how many BLE devices (mainly smartphones) around Raspberry Pi. We tried to scan and count BLE devices from Nodejs.

First, we installed nodejs. We choose node v.8.x because other version didn’t install some BLE library (noble).

1
2
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install -y nodejs

Next, we install noble, a nodejs library to control BLE. First, we install related libraries.

1
sudo apt-get install bluetooth libbluetooth-dev libudev-dev

Then, we install noble by npm.

1
 npm install noble

We want scan smartphone devices in 1 minutes intervals and record it into csv files. Then, we also add date-utils and fs libraries.

1
2
npm install date-utils
npm install fs

Then, we write this JavaScript code:devscan.js

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
var devid = "305"
var noble = require('noble');
require('date-utils');
var fs = require('fs');
var sessionnum = 1;

noble.on('stateChange', function(state){

    if(state == 'poweredOn'){

    noble.startScanning();

    }else {

    noble.stopScanning();

    }

    setInterval(function(){

    noble.stopScanning();
    //console.log("scan stop");

    if(state == 'poweredOn'){
        noble.startScanning();
        sessionnum = sessionnum + 1;
        //console.log("Restart Scanning.");
    }else {
        noble.stopScanning();
    }

    },60000);

});


noble.on('discover', function(peripheral){

    var tx = -67;
    if(peripheral.advertisement.txPowerLevel !== undefined){
    tx = peripheral.advertisement.txPowerLevel;
    }
    var d = Math.pow(10.0,(tx - peripheral.rssi) / (10 * 2));
    var dt = new Date();
    var f = dt.toFormat("YYYY-MM-DD HH24:MI:SS");
    var mfd = "";
    var localname = "";
    if(peripheral.advertisement.manufacturerData){
    mfd = peripheral.advertisement.manufacturerData.toString('hex');
    }
    if(peripheral.advertisement.localName){
    localname = peripheral.advertisement.localName;
    }

    var filename = "/home/pi/fabacademy/" + dt.toFormat("YYYYMMDD_HH24") + ".csv";
    var writeData = devid + "," + sessionnum + "," + f + "," + peripheral.address + ","+ peripheral.addressType + ","
    + peripheral.uuid + "," + mfd + "," + peripheral.rssi + "," + tx + "," + d + "\n";
    appendFile(filename,writeData);


});


noble.on('warning', function(message){
//    console.log("warning -> " + message);
    noble.startScanning();

});


function appendFile(path,data){

    fs.appendFile(path,data,function(err){

    if(err){
        throw err;
    }

    });


}

Here is the result:

Impression

We can do anything we want easily on Raspberry Pi. There are a lot of options we can choose for programming. Also, it is pre-installed some communication modules such as Wi-Fi, Bluetooth and Ethernet. We can add camera modules and control it very easily.

However, the most difficult part to use raspberry pi is to setting up it. To setup raspberry pi, it is required basic knowledge or experience of Linux OS. Also, Raspberry Pi is just a board. When setting up the Raspberry Pi, we need HDMI display, mouse and keyboard. Otherwise, as following like this page, we need to setup SSH communication.