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.

  1. 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.

  2. Launch your Arduino IDE on your computer.

  3. Click on the Tools menu and select Board: Arduino Uno. (It may be listed as “Arduino Uno (Rev 3)”.)

  4. Again in the Tools menu, make sure the Port for Arduino Uno is selected. On macOS, this will likely be something like dev/cu.usbmodem/146301. On Windows, this will likely be something like COM4.

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.)

Arduino board with LEDs highlighted

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!


Installing 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.

Installation of Python distribution

We now proceed to install a Python distribution for your machine.

macOS users: Install Command Line Tools

If you are using macOS, you should install Command Line Tools. This is a set of utilities, such as compilers and (most importantly for this bootcamp) Git. You could install it along with the very bloated software XCode, but we will not do that to save on the ≈35 GB of disk space that XCode will consume.

To install Command Line Tools, open the Terminal application. It is typically in the /Applications/Utilities folder. Otherwise, hit ⌘-space bar and type terminal in the search box, and select the Terminal Application. Once you have a prompt in the Terminal application, type

xcode-select --install

and hit enter. You will be prompted for the installation, and you should go ahead with it. It may take several minutes for the installation to complete.

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.

Downloading and installing Anaconda

If you have not already, download and install Anaconda. To do so,

  1. Go to the Anaconda’s download page and click the Download button.

  2. Follow the on-screen instructions for installation. While doing so, be sure that Anaconda is installed in your home directory (which is the default), not in root.

Setting up a conda environment

To set up the conda environment for this class, first, download the YML specification for the environment:

You can then set up the environment on the command line by entering

conda env create -f be189.yml

Make sure you are in the directory where the be189.yml file lives. Henceforth, you should make sure you have that environment active when doing the work for this class. You can activate the environment by entering

conda activate be189

on the command line. Be sure you launch JupyterLab from the same shell in which you activated the environment.

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)
Loading BokehJS ...

Computing environment

[2]:
%load_ext watermark
%watermark -v -p numpy,bokeh,jupyterlab
Python implementation: CPython
Python version       : 3.11.4
IPython version      : 8.12.2

numpy     : 1.24.3
bokeh     : 3.2.1
jupyterlab: 4.0.5