Physical Computing using Jupyter Notebook | by Marcelo Rovai | Towards Data Science

Learn how to install Jupyter Notebook on a Raspberry Pi, and directly on it, read sensors and act on actuators.
We all know that Jupyter Notebook is a fantastic tool, or better, an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text.
Jupyter Notebook is largely used in Data Science, cleaning and transforming data, doing numerical simulation, statistical modeling, data visualization, machine learning, and much more!
But, how about to use Jupyter Notebooks to control Raspberry Pi GPIOs?
That’s is what we will do in this tutorial. We will learn how to install Jupyter Notebook on a Raspberry Pi, and directly on it, we will read sensors and act on actuators.
The diagram gives us an an overview about the project:
And the movie can give you a hint of what is possible to get when the Pi meets Jupyter!
Raspberry Pi V3
DHT22 Temperature and Relative Humidity Sensor
DS18B20 Waterproof Temperature Sensor
BMP180 Barometric Pressure, Temperature and Altitude Sensor
LEDS (3x)
Push Button (1x)
Resistor 4K7 ohm (2x)
Resistor 1K ohm (1x)
Resistor 330 ohm (3x)
Protoboard and cables
Let’s follow the above electrical diagram and connect all 3 sensors, buttons and LEDs to Raspberry Pi.
The sensors used here in this project, are the same used on my tutorial:
IoT Weather Station With RPi and ESP8266
I suggest that you give a look on that tutorial, where I explain one by one, how to install the sensors and their libraries, testing them individually before running the complete program.
To install Jupyter on your Raspberry (that will run with Python 3), open Terminal and enter with following commands:
sudo pip3 install jupytersudo ipython3 kernelspec install-self
Now on your terminal, run the command:
jupyter notebook
And that’s it!!!! Amazing! very simple and easy. The Jupyter Notebook will be running as a server on:
http:localhost:8888
But, this is not really important for you to know, because, automatically your default browser will be opened on that address, running a “Home Page”.
To stop the server and close the “kernels” (the Jupyter notebooks), you must use [Ctrl] + [C] from your keyboard.
From now one, any time that you start your Pi and want use Jupyter Notebook, just type the command “Jupyter notebook” on your terminal and keep it running all the time. This is very important! If you need to use the terminal for another task, as run a program, for example, open a new Terminal window.
We are assuming that you are familiar with Jupyter Notebook. It will work on your Pi exactly as it works on your desktop.
There are “tons” of good Jupyter Notebook tutorials available on the web, just goggle it and you certainly will find one more suitable for you.
Let’s play with a notebook that I have already created for this tutorial.
Download it from my GitHub: Weather Station Sensors — Actuators
Save the notebook on your Raspberry Pi. In my case, I save it on my main directory (see the homepage printscreen above). Observe that exist 4 notebooks in my main directory (notebooks have a “small notebook” as an icon). Just click on it. The notebook will be loaded and will appear as the one above. If you see the homepage again, you will realize that the “notebook icon” is now “green”, that means that it is running.
Also, note that there is a picture of the HW at first cell of my notebook. For the picture to appear correctly, you must or have it in the same directory (that is my case) or change its path on the notebook if you uses another directory to store the picture.
You can enter with Unix commands directlly on a Notebook cell, same as you do in your Terminal
For example, the command:
ls
Will list all your files in the current directory where the notebook is running, as you can see here:
So, the idea is to start writing your script in the same way (and order) that you are used to do with a Python IDE.
I will split my notebook into 3 phases, where we will do:
Initialization
Initialization
Import Libraries
Define GPIOs
Initialize GPIOs
Create important functions
2. Reading Sensors
3. Reading and Acting on GPIOs (Button and LEDs)
Important to note that usually, you will run the Initialization phase only once. After that, you can play with your sensors and GPIOs, running only the pertinent cells created for Phase 2 and 3, as in this example.
For phase 1 better understand, you can follow the introduction movie.
Running the Cell [11] you will get all sensors data:
Widgets
Here it is important to call attention to the “Widgets”.
Widgets are eventful python objects that have a representation in the browser, often as a control like a slider, textbox, etc. You can use widgets to build interactive GUIs for your notebooks. You can also use widgets to synchronize stateful and stateless information between Python and JavaScript.
Here in this example, we are using widgets (“Sliders”) to on cell [12}, control in REAL TIME the state of actuators, in this case, turning on or off the LEDs. Widgets are great to add more dynamic behavior on Jupyter Notebooks.
As you saw, on the code, we must run the cell [11] any time that we want an update value of sensors, but of course, a widget could also be implemented here to do it automatically, or by pressing a button, for example. I will leave it as a challenge for you! ;-)
Installation
In order to use Widgets, you must install the Ipywidgets library. For that use below commands:
sudo pip3 install ipywidgetsjupyter nbextension enable --py widgetsnbextension
After installation, only call the library on your Jupyter Notebook.
Note that we have done it, in the beginning, at initialization phase :
# widget libraryfrom ipywidgets import interactiveimport ipywidgets as widgetsfrom IPython.display import display
This Widget, “interactive”, is simple to be implemented and very powerful. You can learn more about Interactive on this link: Interactive Widget.
I am starting new journeys on this fascinating world of technology. Everything that we see today has data related and knowing better those data can be key to relevant projects that can help people. You will see more and more, here and on my blog MJRoBot.org, tutorials where DataScience is mixed with Physical Computing, Computer Vision, Machine Learning and IoT.
As an example, I played with data from a previous IoT project, the ArduFarmBot :
Using the data that was logged on a IoT Platform, the ThingSpeak and appliyng Machine Learn on those data, was possible to predict (in fact confirm) when the lamp and the pump should be turned on.
The results were amazing!!!!!!
You can see the Jupyter Notebook and the final report on my GitHub: ArduFarmBot_Data_Analysis
That’s all, folks! ;-)
As always, I hope this project can help others find their way into the exciting world of electronics!
For details and final code, please visit my GitHub depository: Pyhon4DS/RaspberryPi
For more projects, please visit my blog: MJRoBot.org
Stay tuned! Next tutorial we will send data from a remote weather station to a central one, based on a Raspberry Pi Flask Web server:
Saludos from the south of the world!
See you in my next tutorial!
Thank you,
Marcelo
No rights reserved
 by the author.