1 of 63

Intro to Hardware Hacking

Eric Bezzam

2 of 63

DISCLAIMERS!!

3 of 63

When we say hardware hacking…

Breaking into the servers of a government / multinational company.

Putting a bug/virus in your arch-enemy’s machine.

Modifying electronics into a form that was not originally intended.

Creating a hardware solution with various parts.

4 of 63

We have food but…

5 of 63

Co-founder of LauzHack (2016).

Follow me on Medium (@bezzam) and Twitter (@EricBezzam).

CS PhD student @ EPFL.

Prev. tech evangelist at Snips / Sonos.

Welcome! My name is Eric Bezzam.

1

2

3

6 of 63

Some things I do with hardware (lensless camera)

Full tutorial on Medium: https://go.epfl.ch/lenslesspicam

RPi HQ sensor

RPi

Arduino

7 of 63

Outline

  1. Arduino vs. Raspberry Pi
  2. Raspberry Pi setup
  3. Connecting to the Raspberry Pi
  4. Connecting various components (camera, mic, screen)
  5. Arduino (if time)

8 of 63

Before we start…

9 of 63

Terminology

  • Pi
  • Local machine
  • Server
  • Flash
  • Hostname
  • IP address
  • SSH
  • VNC
  • GPIO
  • CLI
  • I2S
  • SPI
  • Etc…

Knowing the terms helps to find in the manual and/or Google!

10 of 63

Command line interface

When you see something on the Terminal like:

Do you want to continue? [Y/n]

You are expected to give an answer. Pressing “Enter” will do the default (capital letter).

Otherwise enter your option and then press “Enter”.

11 of 63

Arduino vs. Raspberry Pi

Microcontroller (uC).

No need for operating system.

Good for repetitive and/or deterministic tasks (e.g. opening doors, switching lights).

Typically use Arduino IDE to program (C/C++).

No built-in wireless connectivity.

Can plug and unplug freely.

Single board computer (SBC).

Requires operating system (Raspberry Pi OS).

Good for performing multiple and/or complicated tasks (e.g. sending an email based on sensor reading).

Can develop applications in many languages, as long as RPi can compile.

Hardware for Wi-Fi and Bluetooth on board.

Properly shutdown after use / before powering down.

12 of 63

13 of 63

Flashing the board

14 of 63

What do you need?

  • Laptop with SD card slot.
  • Raspberry Pi.
  • Power supply for Raspberry Pi.
  • microSD card (at least 8GB) and adapter.
  • Wireless local network + credentials.

15 of 63

  1. Install Raspberry Pi Imager

16 of 63

2) Choose OS

17 of 63

3) Plug in SD card and choose the storage device

18 of 63

4) Set advanced settings (WiFi and SSH)

“Gear” button or “Ctrl + Shift + X” shortcut.

19 of 63

Enable SSH and configure WiFi

With multiple Raspberry Pi’s set unique hostname.

20 of 63

5) Write!

Safely eject SD card from file manager.

21 of 63

(Advanced) setting WiFi through wpa_supplicant.conf

To add multiple WiFi networks or to add a WPA enterprise WiFi (e.g. university WiFi) you will have to edit the wpa_supplicant.conf file:

  • Plug in the SD card.
  • Save a file called wpa_supplicant.conf in the “boot” folder with content similar to this file (change username and password).

Switching between multiple networks from the Pi (link). Note that your SSH connection will break.

22 of 63

Connecting without a monitor (headless)

23 of 63

Step 1: Insert the microSD card and power up RPi

24 of 63

Approach #1: Secure shell (SSH)

From Terminal window (replace with your hostname):

$ ssh pi@raspberrypi.local

Enter password if you set one.

See this article for how to avoid entering password each time.

Log off with “Ctrl + D” or shutdown (from Pi!!) with:

$ sudo shutdown -h now # -r to reboot

25 of 63

Approach #2 : Virtual network computing (VNC)

Steps:

  1. Enable VNC server on Raspberry Pi.
  2. Determine IP address of Raspberry Pi.
  3. Download VNC viewer on local machine.
  4. Connect!

26 of 63

  1. Enable VNC server

$ sudo raspi-config

From Raspberry Pi:

Select “Interface Options”, then “VNC”. Select “Yes” to enable the VNC server.

27 of 63

2) Determine IP address of Raspberry Pi

pi@raspberrypi:~ $ ifconfig

eth0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500

...

...

...

lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536

...

...

...

wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500

inet 192.168.XXX.XXX netmask 255.255.255.0 broadcast 192.168.1.255

...

...

...

28 of 63

3) Download VNC viewer on your computer

29 of 63

4) Enter hostname / IP address and connect

30 of 63

Congrats!

All set up!

31 of 63

Which accessories / components?

Depends on application.

Adafruit great place with clear tutorials: https://www.adafruit.com

Example: smart speaker with visual interface.

Microphone(s)

Speaker

Screen

Camera

32 of 63

Typical steps

  1. Connect component while Raspberry Pi is not powered.
  2. Update Pi software (then reboot).

  • Install dependencies.
  • Install driver software (then typically reboot).

sudo apt-get -y update

sudo apt-get -y upgrade

sudo shutdown -r now

33 of 63

Camera

34 of 63

  1. Connecting the camera
  1. Locate the camera module port. (close to jack).
  2. Gently pull up on the edges of the port’s plastic clip.
  3. Insert the Camera Module ribbon cable; make sure the connectors at the bottom of the ribbon cable are facing the contacts in the port.
  4. Push the plastic clip back in place.

35 of 63

2) Power up, enable the camera, and reboot

$ sudo raspi-config

From Raspberry Pi:

Select “Interface Options”, then “Camera”. “Legacy Camera” for Buster OS.

36 of 63

3) Test via command line

Raspberry Pi commands: raspistill, raspivid

(From Raspberry Pi) take a picture and save to file:

$ raspistill -o image.jpg

(From local computer) copy file from Raspberry Pi.

scp pi@raspberrypi.local:image.jpg ~/Desktop/image.jpg

37 of 63

4) Control with Python

Using picamera library (or picamerax for latest developments).

Create a file called simple_capture.py with vim or nano:

sudo apt-get install vim # if not installed

vim simple_capture.py

This will open the editor.

38 of 63

Mini vim tutorial

Full cheatsheet: https://vim.rtorr.com/

  • Enter Edit mode: press “i” (should see “-- INSERT --” in bottom line)
  • Paste text (in Edit mode): “Ctrl” + “Shift” + “V”
  • Exit Edit mode: press “esc”

Outside of Edit mode (should see the command in bottom line):

  • To quit and save: “:x” + Enter
  • To quit and discard changes: “:q!” + Enter

39 of 63

4) Control with Python (continued)

Copy (Ctrl + Shift + V) following code into simple_capture.py:

Run script:

$ python simple_capture.py

from time import sleep

from picamera import PiCamera

camera = PiCamera()

camera.resolution = (1024, 768)

camera.start_preview()

# Camera warm-up time

sleep(2)

camera.capture('foo.jpg')

40 of 63

GPIO components

41 of 63

GPIO: General purpose input/output

Power down Raspberry Pi when connecting components!

42 of 63

First component: MEMS microphone

43 of 63

  1. Wiring (link)

44 of 63

2) Power up Raspberry Pi and install software

# 1) Update Pi software (then reboot)

sudo apt-get -y update

sudo apt-get -y upgrade

# 2) Install dependencies

sudo apt install python3-pip

cd ~

sudo pip3 install --upgrade adafruit-python-shell

# 3) Install driver software (then reboot)

wget https://raw.githubusercontent.com/adafruit/Raspberry-Pi-Installer-Scripts/master/i2smic.py

sudo python3 i2smic.py

For the I2S mic (link):

45 of 63

3) Check installation

Linux typically relies on ALSA (Advanced Linux Sound Architecture) for audio I/O.

Useful commands: arecord, aplay, alsamixer

List devices to check for proper setup:

pi@raspberrypi:~ $ arecord -l

**** List of CAPTURE Hardware Devices ****

card 1: sndrpii2scard [snd_rpi_i2s_card], device 0: simple-card_codec_link snd-soc-dummy-dai-0 [simple-card_codec_link snd-soc-dummy-dai-0]

Subdevices: 1/1

Subdevice #0: subdevice #0

46 of 63

4) Test!

Record 5 seconds to WAV file using detected card number:

arecord -D plughw:1 -c 1 -r 16000 -f S32_LE -t wav -d 5 file.wav

Playback (connect headphones to jack):

aplay file.wav

Or copy to computer.

scp pi@raspberrypi.local:file.wav ~/Desktop/file.wav

47 of 63

Second component: OLED screen

48 of 63

  1. Wiring (link)

49 of 63

2) Power up Raspberry Pi and install software

Install CircuitPython (link) and various dependencies (link).

# 1) Update Pi software (then reboot)

sudo apt-get -y update

sudo apt-get -y upgrade

# 2) Install dependencies

sudo apt install python3-pip

sudo apt-get install fonts-dejavu

sudo apt-get install python3-pil

sudo pip3 install --upgrade setuptools

cd ~

sudo pip3 install --upgrade adafruit-python-shell

wget https://raw.githubusercontent.com/adafruit/Raspberry-Pi-Installer-Scripts/master/raspi-blinka.py

sudo python3 raspi-blinka.py

# 3) Install driver software

sudo pip3 install adafruit-circuitpython-rgb-display

50 of 63

3) Check installation

You can check that I2C and SPI are properly enabled (link).

And run the Blinka test (link).

pi@raspberrypi:~ $ python3 blinkatest.py

Hello blinka!

Digital IO ok!

I2C ok!

SPI ok!

done!

51 of 63

4) Test!

There are a few test scripts provided here.

We’ll try second one for drawing shapes and text (link).

Be sure to uncomment the line for the corresponding display (1.27" SSD1351 in our case).

I’ve also set BORDER=10 and FONTSIZE=14.

Create Python script on Raspberry Pi and run:

python3 draw_shapes_and_text.py

52 of 63

Arduino

53 of 63

Install IDE

54 of 63

Launch IDE and plug in Arduino

Arduino properly detected

Setup code (runs once at startup)

Code that loops after setup.

55 of 63

Not detecting?

“Tools” and check/set “Board and “Port”.

STILL STUCK?!

56 of 63

57 of 63

Examples

File > Examples > 01. Basics > Blink. → Blink the on-board LED.

58 of 63

Flash board / run code

“Verify” to check that code compiles.

“Upload” to upload code to the board, aka flash.

Blinking LED.

59 of 63

Connecting components?

Similar to Raspberry Pi!

60 of 63

To sum up

61 of 63

Takeaways

  • How to connect to a Raspberry Pi / Arduino.
  • How to connect a component.
  • How to interact with component.

When in doubt? RTFM and Google.

62 of 63

For more LauzHack events (and pizza)

Sign-up to our mailing list: go.epfl.ch/lauzhackmailing

Follow us on Twitter (@LauzHack).

Join committee!

63 of 63