0. Getting set up
In this lesson, you will set up your computer for programming your Arduino board and controlling it with Python-based tools. You will also use Jupyter notebooks for preparing your write-ups.
In this class, we will use Anaconda, with its associated package manager, conda
. It has become the de facto package manager/distribution for scientific use. To use it, and also to serve up Bokeh apps to control your devices, you should have access to a Terminal program on your computer and have basic skills for using the command line. If you want to learn more about the command line, you can check out this
lesson. (In that lesson, JupyterLab’s built-in terminal was used, which you can used. You can use the same, but it is probably better to use a suitable terminal program on your OS.)
The Arduino IDE
You will use the Arduino interactive development environment (Arduino IDE) to write and compile code for Arduino and upload it to your board. To install it, follow the instructions on the Getting Started with Arduino page. You will be given a choice of using an online IDE or the desktop IDE. I recommend the desktop IDE. You can download and install the most recent stable version of the desktop IDE here.
Once you have downloaded the IDE, follow the IDE installation instructions for your operating system.
You should then have a functioning Arduino IDE on your machine.
Follow-along exercise 0: Testing Arduino
This is one of the follow-along exercises for which you are not required to submit anything. This is a test to make sure your Arduino works properly.
Connect your Arduino to your computer using the USB male A-male B cable. Wait a few seconds and then press and release the
RESET
button on the Arduino.Launch your Arduino IDE on your computer.
Click on the
Tools
menu and selectBoard: Arduino Uno
. (It may be listed as “Arduino Uno (Rev 3)”.)Again in the
Tools
menu, make sure the Port for Arduino Uno is selected. On macOS, this will likely be something likedev/cu.usbmodem/146301
. On Windows, this will likely be something likeCOM4
.
When you launch the IDE, you will get a window that looks like this:
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
It contains a sketch, which is the term for code that is compiled and uploaded to Arduino. Delete the default contents of the sketch and instead paste the following into the sketch window.
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(250);
digitalWrite(LED_BUILTIN, LOW);
delay(250);
}
You will come to understand what this does in coming lessons, but for now, we just need to check whether your connection to Arduino is working and whether the board is functioning.
You will need to save this sketch. You can choose whatever file name you want, but it should be descriptive. I also recommend setting up a folder on your machine where you save your materials for this class. The suffix for Arduino sketches is .ino
.
You will watch your Arduino board to make sure everything is functioning properly. In particular, pay attention to the built-in LEDs, highlighted on the board drawing below. (There is another built-in LED, labeled ON
, which is illuminated when the board has power.)
After you have saved the file, you need to compile and upload the code (referred to as a “sketch”) to Arduino. You can do this by clicking on the rightward-pointing arrow at the top of the Arduino IDE window. You will get some messages at the bottom of the Arduino IDE window telling you about the success of the compilation-and-upload process and the storage footprint of your sketch. You will also see the TX
and RX
lights flash as the sketch is being uploaded.
Once the sketch has been uploaded, you will see the LED labeled L
flash at a rate of twice per second. If you see this, it means you have successfully uploaded your sketch and your Arduino board is successfully executing it!
Installation of Anaconda
We now proceed to install Anaconda, which provides a Python environment for your computer.
macOS users: Install XCode
If you are using macOS, you should install XCode, if you haven’t already. It’s a large piece of software, taking up about 5GB on your hard drive, so make sure you have enough space. You can install it through the App Store.
After installing it, you need to open the program so that it will be accessible to your Anaconda-installed packages. Be sure to do that, for example by clicking on the XCode icon in your Applications folder. Upon opening XCode, it may perform more installations. After opening XCode and installing whatever additional installations it prompts you to do, you can close XCode.
Windows users: Install Chrome or Firefox
We will be using JupyterLab in class. It is browser-based, and Chrome, Firefox, and Safari are supported. Microsoft Edge is not. Therefore, if you are a Windows user, you need to be sure you have either Chrome or Firefox installed.
Uninstalling Anaconda
If you have previously installed Anaconda with a version of Python other than 3.9, you may want to uninstall it, removing it completely from your computer. You can find instructions on how to do that from the official uninstallation documentation.
Having a fresh installation on your machine will help us work together, troubleshoot, etc.
Downloading and installing Anaconda
Downloading and installing Anaconda is simple.
Go to the Anaconda distribution homepage, click “Download” and download the graphical installer. Be sure to download Anaconda for Python 3.8 for the appropriate operating system.
Follow the on-screen instructions for installation. When prompted, be sure to select “Install for me only.”
You may be prompted for optional installations, like PyCharm. You will not need those.
That’s it! After you do that, you should have a functioning Python distribution.
The conda package manager
conda is a package manager for keeping all of your packages up-to-date. It has plenty of functionality beyond our basic usage in class, which you can learn more about by reading the docs. We will primarily be using conda to install and update packages.
conda works from the command line. The first thing we’ll do is update the packages that came with the Anaconda distribution. To do this, enter the following on the command line (yes, do the first command twice):
conda update conda
conda update conda
conda update --all
You will be prompted to perform the updates, and press y
to continue. (If everything is up to date, you will just see a list of all the installed packages.) There may even be some downgrades. This happens when there are package conflicts where one package requires an earlier version of another. conda is very smart and figures all of this out for you, so you can almost always say “yes” (or “y
”) to conda when it prompts you.
Installations
You should first install node.js. node.js is a platform that enables you to run JavaScript outside of the browser. We will not use it directly, but it needs to be installed for some of the more sophisticated JupyterLab functionality. Install node.js by following the instructions here.
There are a few additional installations you need to do. Most importantly, you need to install pyserial, which enables serial communication with your device.
conda install pyserial panel
You should also install jupyter_bokeh
to enable Bokeh to function in a Jupyter notebook.
conda install -c bokeh jupyter_bokeh
Finally, it is convenient to have a browser-based dashboard for control of your Arduino board. You can install that as follows.
pip install --upgrade serial-dashboard
Optional installations
Black comes pre-installed with Anaconda. It enables convenient code formatting to help keep your code clean. To use Black with JupyterLab, you can install blackcellmagic.
pip install blackcellmagic
I also encourage you to install watermark, which enables you to quickly determine and display which versions of various packages you have installed.
pip install watermark
You may also wish to install a spell-checker. To do so, execute the command below on the command line.
pip install jupyterlab-spellchecker
Checking your Python distribution
We’ll now run a quick test to make sure things are working properly. We will make a quick plot that requires some of the scientific packages you just installed.
Launch JupyterLab, preferably from the command line. using
jupyter lab
If you want to specify the browser, you can, for example, type
jupyter lab --browser=firefox
Once you have a JupyterLab window open in your browser, use the JupyterLab launcher (you can get a new launcher by clicking on the +
icon on the left pane of your JupyterLab window) to launch a notebook. In the first cell (the box next to the [ ]:
prompt), paste the code below. To run the code, press Shift+Enter
while the cursor is active inside the cell. You should see a plot that looks like the one below. If you do, you have a functioning Python environment for scientific
computing!
We will check your serial connections soon as we start connecting Arduino to your machine.
[1]:
import numpy as np
import bokeh.plotting
import bokeh.io
bokeh.io.output_notebook()
# Generate plotting values
t = np.linspace(0, 2 * np.pi, 200)
x = 16 * np.sin(t) ** 3
y = 13 * np.cos(t) - 5 * np.cos(2 * t) - 2 * np.cos(3 * t) - np.cos(4 * t)
p = bokeh.plotting.figure(height=250, width=275)
p.line(x, y, color="red", line_width=3)
text = bokeh.models.Label(x=0, y=0, text="biodevices", text_align="center")
p.add_layout(text)
bokeh.io.show(p)
Computing environment
[2]:
%load_ext watermark
%watermark -v -p numpy,bokeh,jupyterlab
Python implementation: CPython
Python version : 3.8.11
IPython version : 7.27.0
numpy : 1.20.3
bokeh : 2.3.3
jupyterlab: 3.1.7