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:

ArduinoAllinOne
  • 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 is to be done before coming to the lab, and it is a part of the logbook.

  • The logbook will be submitted through Gradescope the next day after the lab @ 05:00 pm.

  • In our lab sessions, we have an instructional part and a practice part. Please pay attention to the explanation carefully.

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 preperation 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 750 (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 = 750$) 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.

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"

show applications
  • Search for "Thonny" at the search bar, and

  • Select the appropriate icon

thonny

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…​"

thonny manage

  • Type "engi1020" at the search bar and click "Find package from PyPi"

thonny find
  • Click "Install"

thonny 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!

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, 750]. There are no units for the raw sensor measurement. We will use the output readings to control the intensity 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

To print the value directly to the RGB LCD screen, we can call the following function with the variable name directly, without quotation marks. However, if we would like to print a sentence with the readings, we can use the following:

rgb_lcd_print(f'Light readings: {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, if I want to display a white colour, I will use the following:

rgb_lcd_colour(255, 255, 255)

Q1: In your logbook, record the coding part for steps 1, 2, 3, and 4)

Q2: Did you encounter any errors? What are they?

Q3: How did you solve them?

5- Let’s make things more fun!

Using a similar way of mapping the output of an analog sensor to colors, let’s work with more variations!

RGB LCD

Instead of the light intensity automation feature, here we are looking into a colour picker feature. This can be accomplished using a different analog sensor, the rotary dial. The readings range of the rotary dial sensor is between 0 and 1023 (integer values). 0 and 1023 represent the extreme ends of the sensor’s position (all the way to the left or all the way to the right). We can read the dial output by using the same function:

analog_read(pin)

However, the pin here would be different, it is the number of the analog port to which the dial is connected.

Since the range of the input sensor is different, we will need to re-implement the mapping expression that we got during the preparation.

Q4: Modify the expression and note it in your logbook.

Our aim in this step is that, as we change the positions of the dial, we observe the colour change in the LCD screen, following the RGB color wheel pattern.

To simplify this step, we will not walk through the whole color wheel, as this will require changing red, green, and blue at different segments/combinations. Therefore, choose a quarter that you would like to observe, and move on with the implementation from there!

For example, if I wanted to observe the first segment (i.e., red to yellow), I will call the function while fixing red and blue ranges, and vary the green colour, as can be observed in the figure above. The function call will look like this: rgb_lcd_colour(255, green , 0).

Q5: In your logbook, record the chosen segment and note which colours should remain constant according to the figure above

Q6: In your logbook, record the coding part for step 5

Q7: Did you encounter any errors? What are they?

Q8: 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!