Lab 1: Arduino with sensors and actuators using triggers and filters
- Due 28 Mar 2022 by 23:59
- Points 1
- Submitting a text entry box, a media recording, or a file upload
- Available 21 Mar 2022 at 9:00 - 25 Jul 2022 at 23:59
In this lab, you will get familiarised with your kit and with programming a basic interaction in Arduino using sensors, actuators, and applying a filter to improve the readings.
0. Preparatory tasks (to be completed before the start of the lab session, otherwise you will run out of time):
-
Have at hand the Arduino basics page (link will open on this tab) and quickly skim its content, it might be handy later during the lab session. You can always find it also on Canvas, Home > Resources.
- Also have at hand the Arduino website: https://www.arduino.cc Links to an external site..
- Download the Arduino IDE: https://www.arduino.cc/en/software Links to an external site. and familiarise with it.
- Open the Arduino kit in your goodies bag and take a look at the material. Let us know asap if you're sure something is missing from your bag. The list of materials is on the slides from lecture 1.
- Watch this wiring basics tutorial for Arduino: https://www.youtube.com/watch?v=vEHlwq3CHk4
Links to an external site.
- Complete this tutorial to create your first Arduino project: https://www.allaboutcircuits.com/projects/learn-how-to-use-the-arduinos-digital-i-o/ Links to an external site..
- Pay attention to how you connect the LED to the breadboard. Hint: forward bias.
- If the Arduino IDE is already open when you connect the USB cable, it might not find the correct serial port. After reopening the Arduino IDE, select the correct port under "Tools".
1. Lab procedure (you can start this before the lab session):
- Pick 2 sensors and 1 actuator from your goodies bag*. Figure out what they are and how they work. Do not choose the radio transceiver or the accelerometer. Do not use a monochromatic LED as in the example given, try cooler stuff! (you can use an RBG LED if you want to).
- Connect and “read” the sensors by programming the Arduino.
-
The interaction you program cannot be a direct mapping from the sensors to the actuators. You should implement trigger conditions and filtering (see more info below).
Optional point: Was this too easy? If so, you can choose to push yourselves into making a) a more utilitarian use of your sensors and actuators (i.e. could they help achieve a certain goal?) OR b) a more creative use of your sensors and actuators (i.e. can you come up with a cool, fun interaction that is not mainstream and rather artistic?)
- Shoot and upload a short video that follows these instructions (if you don't follow them, we won't be able to assess your submission, and so you'll have to shoot it again):
- Show the circuit. Briefly explain the wiring and the components in use (especially chosen sensors and actuators).
- Walk us briefly through the code, explaining how it works (especially triggers and filters).
- Show the interaction in action, making sure that we can see the computer screen (if something is happening there), the circuit with sensors and actuators, and you interacting with them simultaneously.
-Do not worry about edition details, e.g., background music or transitions are not needed, nor part of the grading criteria.
-The video should not be too long: You can effectively explain this assignment in just a few minutes. For example, you don't need to go through the whole code line by line, since we will have access to it; instead, we ask you to explain what it does, to show us you understand what you have coded.
-Preferably, both students should be active in the videos along the course. It's up to you how to divide the tasks. And, it's ok if you don't want to show your face, but we want to e.g. hear you explaining parts of the assignment (unless there is a good reason not to, which you can disclose to the examiner), to show us that you are both participating in group work, and learning. - Upload your code (the Arduino sketch).
- Write and upload a brief reflection on what you learned and on how your sensors/actuators could be further used for interaction.
- Optional: Did you struggle with any part of the assignment's description? (Not with its resolution, but with the way the instructions were written). If so, you can briefly describe your struggle and your suggestion for improvement, together with the previous files or as a submission comment. Thank you!
* Important:
You will have to figure out how the sensors and actuators work and how they are to be connected, by searching information about them.
Still, here is a hint: Most of the sensors come from these 2 boxes, which will help you identify them more easily (zoom in!).
Trigger conditions:
A trigger condition is basically an "if" statement that defines when to trigger something in your code based on the sensor data. In this example, the LED is lit when the sensor reading from the Analog Input 0, reaches 500 (this value is just an example, it needs to be adjusted for each sensor and environment).
const int SENSOR_PIN = A0; // Arduino pin connected to your analog sensor
const int LED_PIN = 3; // Arduino pin connected to LED's pin
const int SENSOR_THRESHOLD = 500; // THIS NEEDS TO BE TUNED
// variables will change:
int analogValue;
void setup() {
pinMode(LED_PIN, OUTPUT); // set arduino pin to output mode
}
void loop() {
analogValue = analogRead(SENSOR_PIN); // read the input on analog pin
if (analogValue < SENSOR_THRESHOLD)
digitalWrite(LED_PIN, HIGH); // turn on LED
else
digitalWrite(LED_PIN, LOW); // turn off LED
}
Trigger conditions can be made more complex than in this example. For instance, through statements such as if this AND that. Example: Let us say that the trigger should only occur if the user simultaneously presses a button:
if (analogValue < SENSOR_THRESHOLD && buttonPressed)
Filters:
Many sensors do not output stable signals, and the data fluctuates rapidly in the readings, commonly referred to as noise.
Use Serial.println(sensorData) in the loop() function and look at your data using the Serial Plotter under the "Tools" menu. (Remember to also use Serial.begin in your setup). You will likely notice that the readings jump around, even when you are not doing anything with the sensor.
To get smoother readings, we typically apply a filter. For digital sensors, we need to use software filters. For analog sensors, filters can be implemented through software or through hardware.
Software filters:
Easy low pass filter:
To get the smoothed value, you take a percentage of the previous reading (e.g. 94%) and you add to it a small percentage of the new reading (e.g., 6%).
Hints:
You will have to define a global variable in your code for the previous reading, and a constant to define the percentage you want to consider.
You then write your filter in your loop() function: read the value from the sensor in the input pin, calculate the smoothed value, and update the previous value.
Try plotting both your newReading value and your lowpassReading value using the Serial Plotter to see what difference it makes. To do so, you have to print a "," between the values, like this:
Serial.print(newReading);
Serial.print(",");
Serial.println(lowpassReading);
You can watch this video by Tove (last year's TA) Download watch this video by Tove (last year's TA) to see and understand the effect of this filter.
Running average:
With this filter, you simply add the new reading to an array of the last n readings and then you calculate the average, in order to get a smoother signal. Here Links to an external site. is one example of how you can do this for a 10 readings average.
Hardware filters:
First order RC low pass filter (I strongly recommend you to explore this one, even if you also implemented a software filter):
In your starter kit, you should have an assortment of resistors and capacitors. Using these you can easily wire a first order low pass filter as follows:
The combination of the resistor and the capacitor will set the cut off frequency and filter out frequencies higher than that. Use a calculator Links to an external site. to figure out the cut off frequency (you typically want to go low). For instance, if you select a resistor of 1 kOhms and a 470uF capacitor, you will get a low pass filter with a 0.38Hz cut off frequency.
To make it easier for you in your first lab and encourage you to befriend the hardware, we are including this other video explaining how to implement this filter (I recommend you to watch it after trying to do this on your own, though, it will feel nicer!).
2. Lab session:
During the lab session, the teachers will be available to help you if you have got stuck with the assignment.
For those connecting remotely, we will use a queuing system to make it fair and organised. So, if you have trouble completing the lab AND after looking for info online you are still stuck, OR towards the end of the session you simply want to show us what you did, then you sign up here: https://queue.csc.kth.se/ (look for DM1588).
Please indicate there your group number, your Zoom meeting link (make sure it is not password protected) and whether you require help or just want to show us your nice results (this is optional). When it is your turn, a teacher will join your Zoom meeting and will label your group in the queue as being helped.
We also have physical classrooms assigned to us along the course (you can see this on the official schedule in KTH Social). We will do our best to have at least one teacher physically there (maximum 2 teachers) to help those who really want in-person assistance. This will depend on how we assess the risks regarding the pandemic, which may change from one week to another, so please let's be patient with each other and respect social distance, etc. You can also use the classrooms during the assigned slots if you need a space to gather with your group, even if you don't need the teachers' assistance.