Skip to content

Output devices | Safety area

This seventh week I worked on getting used to output devices.

For this assignment we had to develop a program that uses an output device.

I made a program that simulates limits in a danger zone, displaying the distance (taken from an ultrasonic sensor) with a message on the LCD and activating a RGB LED according to the distance:

Logic:

-Program starts:
LED = solid blue
LCD message = “Fundamentals2020 FabLab - KaLi”

message scrolls to the left; the reading of the distance starts

*More than 15cms (safe zone):
LED = solid green
LCD message = “Distance: –cm Safe area”

*Between 10 and 15cms (caution zone):
LED turns yellow
LCD message = “Distance: –cm Limit area”

*Less than 10cms (danger zone):
LED blinks in red
LCD message = “Distance: –cm Danger!”

Software and hardwares

- For this practice we programmed an Arduino UNO with its Arduino IDE.

Components:

For this assignment I used:

Quantity Description
1 x Arduino UNO
1 x HC-SR04 Ultrasonic distance sensor
1 x LCM-S01602DTR/M LCD 16x2
1 x B10K Potentiometer
1 x RGB LED
3 x Resistor 150 Ohms
2 x Small breadboard
a lot Jumper cables

Circuit

Pin configuration:

- HC-SR04 Ultrasonic distance sensor:

Pin Connection
Vcc 5V
GND GND
Echo Arduino Pin 6
Trigger Arduino Pin 7

Circuit

- LCM-S01602DTR/M LCD 16x2:

Pin no. Pin name Connection
1 Vss (ground) GND
2 Vdd (+5V) 5V
3 V0 (Contrast V) Potentiometer Output
4 RS (Register select signal) GND
5 R/W (Read/Write) Arduino Pin 13
6 E (Enable) Arduino Pin 12
11 DB4 (Data Bus 4) Arduino Pin 5
12 DB5 (Data Bus 5) Arduino Pin 4
13 DB6 (Data Bus 6) Arduino Pin 3
14 DB7 (Data Bus 7) Arduino Pin 2

Circuit Circuit

- B10K Potentiometer:

Pin Connection
Vcc 5V
Output LCD Pin 3 (V0)
GND GND

Circuit

- RGB LED:

Pin Connection
G Resistor >> Arduino Pin 11
R Resistor >> Arduino Pin 10
GND GND
B Resistor >> Arduino Pin 9

Circuit

Physical circuit:

Circuit

Code

  • First I made a function that contains a greetings message to be shown on the LCD at the beginning, then I scroll it with the command lcd.scrollDisplayLeft();:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
void Greeting()
{
  // Print first message on LCD
  lcd.setCursor(0,0);
  lcd.print("Fundamentals2020");
  lcd.setCursor(0,1);
  lcd.print(" FabLab - KaLi ");
  RGB_color(0, 0, 255); // Blue
  delay(2500); // wait 2.5 segs

  // Scroll message to the left
  for (int positionCounter = 0; positionCounter < 16; positionCounter++)
  {
    lcd.scrollDisplayLeft();
    delay(150);
  }
  delay(1000); // wait 1 seg
}
  • I think that the most interesting of this code id the RGB LED part, then of it the Green, Red and Blue colors are controlled independently:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
int Glight = 11;
int Rlight= 10;
int Blight = 9; 

void setup()
{
  pinMode(Rlight, OUTPUT);
  pinMode(Glight, OUTPUT);
  pinMode(Blight, OUTPUT);
}
  • In the loop I get the readings from the Ultrasonic sensor, then it calls a function named Result(d):
1
2
3
4
5
6
void loop()
{
  [Getting readings from Ultrasonic sensor]

  Result(d);
}
  • The function Result(d) changes the color according to the distance that the ultrasonic sensor detects; it does it by calling another function named RGB_color(red_light_value, green_light_value, blue_light_value); and giving it the RGB values needed for each color:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
void Result(long d)
{
  if (d<10)
  {
    // Start blinking in red
    RGB_color(255, 0, 0); // High red
    delay(500);
    RGB_color(64, 0, 0); // Low red
    delay(500);
  }
  else if (d>15)
  {
    RGB_color(0, 255, 0); // Green
    delay(100);
  }
  else
  {
    RGB_color(255, 200, 0); // Yellow
    delay(100);
  }
}
  • Finally, the function RGB_color(red_light_value, green_light_value, blue_light_value); writes on every RGB pin the received values:
1
2
3
4
5
6
void RGB_color(int red_light_value, int green_light_value, int blue_light_value)
{
  analogWrite(Rlight, red_light_value);
  analogWrite(Glight, green_light_value);
  analogWrite(Blight, blue_light_value);
}

Full code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <LiquidCrystal.h>
LiquidCrystal lcd(13, 12, 5, 4, 3,2);

// LED_Connection [G R GND B]
int Glight = 11;
int Rlight= 10;
int Blight = 9;

const int Trigger = 7;
const int Echo = 6;

void setup() {
  // LCD setup
  lcd.begin(16, 2);

  // Ultrasonic Sensor setup
  pinMode(Trigger, OUTPUT);
  pinMode(Echo, INPUT);
  digitalWrite(Trigger, LOW);

  // RGB setup
  pinMode(Rlight, OUTPUT);
  pinMode(Glight, OUTPUT);
  pinMode(Blight, OUTPUT);

  Greeting();
}

void loop()
{
  long t; //echo width
  long d; //distance in cm

  // Send 10us pulses to the Trigger
  digitalWrite(Trigger, HIGH);
  delayMicroseconds(10);
  digitalWrite(Trigger, LOW);

  t = pulseIn(Echo, HIGH); //get the pulse width
  d = t/59; //get distance in cm

  Result(d);
}

void Greeting()
{
  // Print first message on LCD
  lcd.setCursor(0,0);
  lcd.print("Fundamentals2020");
  lcd.setCursor(0,1);
  lcd.print(" FabLab - KaLi ");
  RGB_color(0, 0, 255); // Blue
  delay(2500); // wait 2.5 segs

  // Scroll message to the left
  for (int positionCounter = 0; positionCounter < 16; positionCounter++)
  {
    lcd.scrollDisplayLeft();
    delay(150);
  }
  delay(1000); // wait 1 seg
}

void Result(long d)
{
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Distance: " + String(d) + " cm");
  lcd.setCursor(0,1);

  if (d<10)
  {
    lcd.print("    Danger!");
    // Start blinking in red
    RGB_color(255, 0, 0); // High red
    delay(500);
    RGB_color(64, 0, 0); // Low red
    delay(500);
  }
  else if (d>15)
  {
    lcd.print("   Safe area");
    RGB_color(0, 255, 0); // Green
    delay(100);
  }
  else
  {
    lcd.print("   Limit area");
    RGB_color(255, 200, 0); // Yellow
    delay(100);
  }
}

void RGB_color(int red_light_value, int green_light_value, int blue_light_value)
 {
  analogWrite(Rlight, red_light_value);
  analogWrite(Glight, green_light_value);
  analogWrite(Blight, blue_light_value);
}

Demonstration

Download files

- Code: SpeedPosAlt.ino
- LCD datasheet: LCM-S01602DTR/M.pdf
- HC-SR04 example code: HC-SR04_Example.ino


Last update: February 12, 2021