Professional Documents
Culture Documents
Guidelines&Templates
for laboratory works
on the course
"ES as the basis of IoT infrastructure"
2023
2
TABLE OF CONTENT
Laboratory work №1 Getting started with Tinkercad ....................................... 3
Laboratory work №2 Ultrasonic distance sensor and servomotors................... 9
Laboratory work №3 RGB LED and smoke detector ..................................... 15
Laboratory work №4 Photoresistor ................................................................. 21
Laboratory work №5 Motion sensor ............................................................... 24
Laboratory work №6 Temperature sensor ...................................................... 28
Laboratory work №7 Group of sensors........................................................... 32
Sources of useful information ......................................................................... 38
3
Laboratory work №1
Getting started with Tinkercad
After creating an account, a page with projects will open, where you
need to open the tab "Circuits" (Fig.1.2). Here you can review your circuits.
After that you need to push “Create new circuit” button (Fig. 1.3).
Circuit editing page consists of working area, toolbar and
components catalog (Fig. 1.4).
4
void loop()
{
for(int i = 0; i < 6; i++)
{
digitalWrite(i+2, HIGH);
delay(300);
digitalWrite(i+2, LOW);
delay(300);
}
for(int i = 4; i > 0; i--)
{
digitalWrite(i+2, HIGH);
delay(300);
digitalWrite(i+2, LOW);
delay(300);
}
}
Put this code in the editor and click "Start Simulation". If everything
is alright, LEDs will light up one by one (Fig. 1.7).
7
Then change circuit name and save it in brd format (Fig. 1.8).
Brd file can be downloaded on computer. You can also review your
circuit in your profile on "Circuits" tab.
Lets create another project with digital button. Drag and drop
"Arduino Uno R3", "Breadboard small", one button and one resistor (10kOm).
Connect them as it shown in Fig. 1.9.
Write the code from the Fig. 1.9 and click "Start Simulation".
Open the Serial Monitor that is located in the bottom of the “Code”
sidebar (Fig. 1.9). Press the button and see what happens when button is
pressed and not pressed.
8
Laboratory work №2
Ultrasonic distance sensor and servomotors
void setup()
{
Serial.begin(9600);
pinMode(2, OUTPUT);
}
void loop()
{
cm = readUltrasonicDistance(7, 7);
Serial.print(cm);
Serial.print("cm, ");
if (cm > distanceThreshold)
{
digitalWrite(2, LOW);
}
if (cm <= distanceThreshold)
{
digitalWrite(2, HIGH);
}
delay(100);
}
bool b = true;
void setup()
{
Serial.begin(9600);
servo_9.attach(9);
}
void loop()
{
if (b)
{
servo_9.write(180);
b = false;
}
else
{
servo_9.write(-180);
b = true;
}
delay(2000);
}
14
2.3.1 Create circuit as in Fig. 2.4 and Fig. 2.5. Add code, Start
simulation and see what happens.
2.3.2 Add five LEDs. If distance to object is less than 50cm, then all
LEDs turn on. If distance is bigger than 300cm then all LEDs turn off. If
distance is more than 50cm and less than 100cm -> 4 LEDs turn on. If
distance is more than 100cm and less than 150cm -> 3 LEDs turn on. If
distance is more than 150cm and less than 200cm -> 2 LEDs turn on. If
distance is more than 200cm and less than 300cm -> one LEDs turn on.
2.3.3 Add servomotor. Servomotor position by default is 90 degrees.
If distance to object is less than 100cm, then servomotor turns around to 0
degrees, if servomotor more than 100cm turns around to 180 degrees.
Laboratory work №3
RGB LED and smoke detector
Goal: get started with RGB LED and smoke detector, learn to create
Arduino circuits using these components in Tinkecad.
The RGB LED consists of three different LEDs: red, green and blue.
By mixing these colors, we can get many other colors. Arduino has an analog
recording function that receives different colors for Arduino RGB LEDs [3].
There are two types of RGB LEDs: common cathode and common
anode. In RGB LEDs with a common cathode all LEDs are common and the
signal is transmitted to the anode of the LED, while in RGB LEDs with a
common anode the anode of all LEDs is common and the signal is transmitted
to the cathode of the LED [3].
In Tinkercad environment LED with common cathode is used.
In Fig. 3.1 RGB LED scheme is depicted.
In Fig. 3.4 there is a table of primary colors and their RGB codes.
Code of the example:
void setup()
{
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
}
void loop()
{
analogWrite(8, 0); // Green
analogWrite(9, 0); // Blue
analogWrite(10, 255); // Red
}
18
In this example (Fig. 3.5) circuit with smoke detector and LED is
created.
void setup()
{
pinMode(led, OUTPUT);
pinMode(smokeA0, INPUT);
Serial.begin(9600);
}
void loop()
{
int analogSensor = analogRead(smokeA0);
Serial.print("Gas sensor value: ");
Serial.println(analogSensor);
// change the sensor value (reading from 85 to 385) in the range of
[0; 100]
int gas = map(analogSensor, MIN_SENSOR_VALUE, MAX_SENSOR_VALUE, 0,
100);
Serial.print("After mapping: ");
Serial.println(gas);
if (gas > sensorThres)
{
digitalWrite(led, HIGH);
}
else
{
digitalWrite(led, LOW);
}
delay(100);
}
20
3.3.1 Create a circuit as shown in Fig. 3.3 and add code. Change
values of signals and analyze how the color of the RGB LED changes.
3.3.2 Create a circuit as shown in Fig. 3.5 and add the code. Add
LED to the circuit and change the program as follows:
• If the value of the smoke sensor is less than 25 - the LED lights up
green.
• If the smoke sensor value is 25 to 50 - yellow.
• If the smoke sensor value is 50 to 75 - orange.
• If the smoke sensor value is more than 75 - red.
Laboratory work №4
Photoresistor
void setup()
{
pinMode(A0, INPUT);
Serial.begin(9600);
23
pinMode(9, OUTPUT);
}
void loop()
{
// read the value from the sensor
sensorValue = analogRead(A0);
// print the sensor reading so you know its range
Serial.println(sensorValue);
// map the sensor reading to a range for the LED
analogWrite(9, map(sensorValue, MIN_SENSOR, MAX_SENSOR, 255, 0));
delay(100); // Wait for 100 millisecond(s)
}
Laboratory work №5
Motion sensor
Goal: get started with motion sensor, learn to create Arduino circuits
using this component in Tinkecad.
The PIR sensor is structurally divided into two halves. This is due to
the fact that for the alarm device it is important to have movement in the
sensitivity zone, and not the level of radiation [6].
25
void setup()
{
pinMode(sensorPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
void loop()
{
// read the state of the sensor/digital input
sensorState = digitalRead(sensorPin);
// check if sensor pin is HIGH. if it is, set the
// LED on.
26
if (sensorState == HIGH)
{
digitalWrite(ledPin, HIGH);
Serial.println("Sensor activated!");
}
else
{
digitalWrite(ledPin, LOW);
}
delay(100); // Delay a little bit to improve simulation performance
}
Laboratory work №6
Temperature sensor
void setup()
{
pinMode(A0, INPUT);
Serial.begin(9600);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
}
void loop()
{
int sensorValue = analogRead(A0);
// measure temperature in Celsius
temp = (double) sensorValue / 1024; //find percentage of input
reading
temp = temp * 5; //multiply by 5V to get voltage
temp = temp - 0.5; //Subtract the offset
temp = temp * 100; //Convert to degrees
Serial.print(temp);
Serial.println(" C");
if (temp < Temp1) {
digitalWrite(2, 0);
digitalWrite(3, 0);
digitalWrite(4, 255);
}
if (temp >= Temp1 && temp < Temp2) {
digitalWrite(2, 0);
digitalWrite(3, 255);
digitalWrite(4, 255);
}
if (temp >= Temp2) {
digitalWrite(2, 255);
digitalWrite(3, 255);
digitalWrite(4, 255);
}
delay(500); // Wait for 500 millisecond(s)
}
Laboratory work №7
Group of sensors
The soil moisture sensor consists of two probes that measure the
volume of water in the soil. The two probes allow the electric current to pass
through the soil and, according to its resistance, measures the moisture level of
the soil [8].
When there is more water, the soil conducts more electricity, which
means that the resistance will be less. So the moisture level will be higher. Dry
soil reduces conductivity. So, when there is less water, the soil conducts less
electricity, which means it has more resistance. So the moisture level will be
lower [8].
In Fig. 7.1 moisture sensor is depicted.
void setup()
{
Serial.begin(9600);
pinMode(led, OUTPUT);
pinMode(A0, INPUT);
//pinMode(2, OUTPUT);
}
void loop()
{
int moisture = analogRead(A0);
int moisturePercent = map(moisture, MIN_SENSOR_VALUE,
MAX_SENSOR_VALUE, 0, 100);
Serial.println(moisturePercent);
if (moisturePercent > moistureThres)
{
digitalWrite(led, HIGH);
}
else
{
digitalWrite(led, LOW);
}
delay(100);
}
void setup()
{
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
pinMode(A4, INPUT);
pinMode(A5, INPUT);
Serial.begin(9600);
}
void loop()
{
double temperature = getTemperature();
Serial.print("Temperature: ");
Serial.println(temperature);
Serial.println();
delay(1000); // Wait for 500 millisecond(s)
}
double getTemperature()
{
int sensorValue = analogRead(A5);
double temp = (double) sensorValue / 1024; //find percentage of
input reading
temp = temp * 5; //multiply by 5V to get voltage
temp = temp - 0.5; //Subtract the offset
temp = temp * 100; //Convert to degrees
return temp;
bool isMoving()
{
return (bool)digitalRead(A4);
}
int getLuminance()
{
return analogRead(A3);
}
int getGas()
{
37