This lab will allow you to practice writing expressions that perform arithmetic calculations on input data and map the values to an output device.
Introduction
Equipment
In this semester, we will be practicing programming in the lab via the Grove Beginner Kit:
-
The Arduino is a programmable circuit board that is also known as a microcontroller. A microcontroller is a small computer with a processor, memory, and other peripherals designed for embedded applications.
-
The Beginner Kit includes one Arduino compatible Board, 10 additional Arduino sensors and all in one-piece of PCB design. All the included sensors are connected to the built-in Arduino through the PCB stamp holes, so no breadboard, soldering, or even wiring is needed!
-
You can connect extra pieces to the Grove ports using included Grove connector cables (later in the semester we may use additional sensors such as distance sensors, servo motors, or other devices).
-
The Arduino is usually programmed using the Arduino IDE software, which uses a variant of the C++ programming language. However, in our course, we will use Python instead!
Instruction
-
Each lab has a preparation part and a procedure part:
-
The preparation part must be completed before coming to the lab, and will be due on Gradescope before your scheduled lab time.
-
The procedure part, along with the testing parts, will be completed during the lab session.
-
-
The rest of the logbook (which includes the procedure and testing parts) will be submitted through Gradescope the next day after the lab @ 11:59 pm.
-
In our lab sessions, we have an instructional part and an implementation part. Please pay attention to the explanation carefully and do not work on the code meanwhile.
Setting up the Arduino Kit
1. Install firmware
To start communicating with Arduino using Python, we need to install firmware on our Arduinos such that it can understand the Python commands. Further instructions will be provided within the lab session on how to do this.
2. Connecting equipment
Using the USB cable, connect the Arduino to the computer. Once the Arduino is connected to the computer, it should be powered and the green PWR LED should be on.
3. Setting Up Python IDE Thonny
In our labs, we will be using Thonny, which is available on our lab computers.
3.1 Open Python IDE Thonny
-
Click "Show Applications"
-
Search for "Thonny" at the search bar, and
-
Select the appropriate icon
3.2 Install engi1020 module in Thonny
Before using our Python interpreter to communicate with the Arduino, we need to install the Python module so that we can use communicate with our Arduino. - Go to "Tools → Manage packages…"
-
Type "engi1020" at the search bar and click "Find package from PyPi"
-
Click "Install"
3.3 Confirm installation was successful
To confirm that the module is installed correctly, implement the following Python statement in the shell of Thonny:
from engi1020.arduino.api import *
Make sure the command was typed exactly as shown. If it runs without error, the module was successfully installed!
Preparation (to be completed before the lab itself)
This lab will use variables and expressions to map an output of the light sensor to color scale on the LCD screen. Much like the smart devices we use daily, which adjust their screen brightness based on the environment, we aim to mimic this intelligent behavior. In doing so, we will practice translating mathematical expressions into Python.
By the end of this lab, you should be able to:
-
Read analog devices connected to the Arduino;
-
Implement a simple mathematical expression in Python;
-
Create a logbook entry that describes your lab activities in context of the questions being asked.
The goal of this preparation is to:
-
Find the mathematical relationship that relates our input sensor (i.e., light sensor) to the desired output (i.e., RGB color). The aim is to scale the light sensor output range to be between 0 and 255. Hence, making it suitable for the color range. You should write the scaling equation for finding red, green, and blue in terms of the light sensor reading. To do that, you can use the following information:
-
It is worth noting that the reading range of the light sensor is between 0 and 800 (integer values).
-
Example: When the environment is too dim (i.e., the light sensor output is zero or near zero ($x_1 = 0$), the red, green, and blue representations should be at the minimum ($y_1 = 0$). When the environment is bright, ($x_2 = 800$) the colors' representations should be at the maximum ($y_2 = 255$).
-
The final mathematical relationship will take the form of $y = m(x - x_1) + y_1$, where m is the slope. Please check this link to revise the concept of finding the equation of a line given two points.
-
-
Come up with a test plan to ensure that your code (that will be written in the lab) is working correctly. List two different ways you can manipulate the light sensor, and what output or change in the output you should observe because of this.
Procedure
1- Reading and storing our input value
Reading our input information in Python using Arduino depends on which input device we use. In today’s lab, we are using an analog sensor (the light sensor). It can output readings in the range of [0, 800]. There are no units for the raw sensor measurement. We will use the output readings to control the brightness of the RGB LCD screen.
First, let us read the light sensor by implementing the statement:
analog_read(pin)
where pin is the number of the analog port to which the device is connected.
This reading that was shown on the screen is not stored anywhere. We are only reading the position at that particular time and printing it. Therefore, if we want to use this value in our mathematical expression, we need to store it. To store information returned from a function, we need to precede the function call with a variable name and assignment operator. For example, we can store values in a variable as follows
Age = 42
seconds_in_day = 60*60*24
Given this information, store the input device readings in a variable.
2- Printing to the RGB screen
If we would like to print a statement with the readings, we can use the following:
rgb_lcd_print(f'Light: {l}')
3- Implementing our mathematical expression
Now, we want to implement the mathematical expression which we derived in the preparation part. As we learned in class, if we have the following mathematical expression
y = 4x + 5
we can represent it in Python as follows:
y = 4*x + 5
Important notes about the example above:
-
You should ensure that $x$ has a value, otherwise, you will get an error.
-
You should ensure to degine a variable in which to store our dependent result (in the above, it is
y
).
Given this information, implement your mathematical expression (from the Preparation) in the Python interpreter.
The background colour of the RGB lcd screen can be changed by specifying how much of the three additive primary colours (red, green and blue) should be present.
4- Changing the colour of the RGB screen
Now, we want to know how to control our output device. We will be changing the background colour of our screen using the rgb_lcd_colour function. This function has three input parameters
-
red (amount of red to display (0–255)).
-
green (amount of green to display (0–255)).
-
blue (amount of blue to display (0–255)).
For example, to display a white color on the RGB screen, you can use the following Python code:
rgb_lcd_colour(255, 255, 255)
Given this, write the Python statement that will enable changing the brightness of the RGB screen based on the light sensor.
Q1: In your script, record the coding part all the above steps.
Q2: Did you encounter any errors? What are they?
Q3: How did you solve them?
Testing
We reached the final step of our implementation! Now we want to test our code thoroughly to ensure that the implementation is correct. Here, we will use the test plan devised during Preparation to determine if our Python code does what is expected.
We will do the following:
-
Read the input value
-
Use your mathematical expression to calculate the output value for red, green, and blue and
-
Manipulate the output device
It will be useful to print the calculated output value to see what your expression evaluated to.
In your logbook (testing section), record what you did physically, what you observed after implementing your statement(s), and whether or not your observations match your expectations in a tabular format.
If you are not getting the expected output from your test, take a systematic approach to debug or determine why it is not working as expected. In your logbook entry, record what changes you make to your statements and what impact that has on the observed output.
If your program is working correctly, congrats! If not, we are here to help!
Make sure to submit the required files for the lab through Gradescope. You will need to submit the following:
-
Lab Logbook (.pdf or .png): Use the provided sample report as a guide
-
Python Script (.py file): This should include the coding part for the lab
On Gradescope, you will find separate repositories for Lab 1. One repository is for submitting the pre-lab, and the other is for submitting the procedure, testing, and Python script completed during and after the lab session. Please make sure to submit your work to the correct repository.