Lesson 5

Line Following Sensor Bar & RGB LED

>> Jump to: 7-in-1 Sensor Bar
>> Jump to: RGB LED Light Module

7-in-1 Sensor BarBack to Top

Robot 7-in-1 Multi-Tracking Sensor Bar

The sensor bar is essential for a line following robot.

It can detect light/dark changes at 5 different locations. There are 4 sensors equally spaced out across the front and one center sensor for assisting when lines cross. You will notice the center sensor is set forward slightly compared to the rest.

There is a bump sensor to trigger when you run into a wall and a special "light sensor" to alert you when something is close.

The line following detection distance is 0 - 4 centimeters.


Here is the order of the sensors.

Left Outer=S1 PIN
Left Inner=S2 PIN
Center=S3 PIN
Right Inner=S4 PIN
Right Outer=S5 PIN



The sensor bar simplifies the process of showing you a "trigger" state with the "built-in" LEDS. To test the sensor bar itself, you can hook up just the VCC and GND wires. As you move your finger across the light detectors, you will see the corresponding LED light will change state on the sensor bar.

Here is the code to test ALL the line following sensors.
void setup() {
  Serial.begin(9600);
}

void loop() {
  if (digitalRead(5) == 1) { Serial.print("S1 "); } else {Serial.print("-  "); }
  if (digitalRead(6) == 1) { Serial.print("S2 "); } else {Serial.print("-  "); }
  if (digitalRead(7) == 1) { Serial.print("S3 "); } else {Serial.print("-  "); }
  if (digitalRead(8) == 1) { Serial.print("S4 "); } else {Serial.print("-  "); }  
  if (digitalRead(12) == 1) { Serial.print("S5 "); } else {Serial.print("-  "); }  
  Serial.println();
  delay(500);
}


Use the Serial Monitor in order to see what is happening in the Arduino output. You should see the output look like this...



Bump Sensor=CLP
Light Sensor=Near



When the bump sensor is triggered, it shows a HIGH signal.
When it is released, it shows a LOW signal.

When an object is close to the Infrared Emitter/Sensor, the pin will show a HIGH signal. When nothing is near, it will show a LOW signal. The adjustable "potentiometer" is used to fine tune the light level detection. The room ambient line is sometimes enough to throw off this particular sensor, so your results may be mixed.

We don't recommend using the Infrared sensor unless absolutely necessary.
It is difficult to fine-tune and will cause most participants a lot of anxiety.

The light sensor on this unit must use the command "digitalRead" instead of "analogRead" since the sensor board has built in logic.


Here is the code to test the BUMP and NEAR sensors.
void setup() {
  Serial.begin(9600);
}

void loop() {
  if (digitalRead(13) == 1) { Serial.print(" Bump"); }
  if (digitalRead(A3) == 1) { Serial.print(" Near"); }
  Serial.println();
  delay(500);
}


Here is the code to run all sensors.
void setup() {
  Serial.begin(9600);
}

void loop() {
  if (digitalRead(5) == 1) { Serial.print("S1 "); } else {Serial.print("-  "); }
  if (digitalRead(6) == 1) { Serial.print("S2 "); } else {Serial.print("-  "); }
  if (digitalRead(7) == 1) { Serial.print("S3 "); } else {Serial.print("-  "); }
  if (digitalRead(8) == 1) { Serial.print("S4 "); } else {Serial.print("-  "); }  
  if (digitalRead(12) == 1) { Serial.print("S5 "); } else {Serial.print("-  "); }  
  if (digitalRead(13) == 1) { Serial.print(" Bump"); }
  if (digitalRead(A3) == 1) { Serial.print(" Near"); }
  Serial.println();
  delay(500);
}


RGB LED Light ModuleBack to Top

In this lesson, you will learn how to use a RGB (Red Green Blue) LED with an Arduino.

The RGB LED can be used to make a Red, Green or Blue light indivually, or a combination of all three colors.


Normally when you write to a pin, you send HIGH or LOW (ie: on/off) using digitalWrite.
This was covered in the Lesson 1 "Blink" sketch.

Turn LED on.
digitalWrite(13, HIGH);

Turn LED off.
digitalWrite(13, LOW);

This code will cycle through all three "RGB" colors using pins 9, 10 & 11.
void setup() {
  pinMode(9, OUTPUT); //RED
  pinMode(10, OUTPUT); //GREEN
  pinMode(11, OUTPUT); //BLUE
}

void loop() {
  digitalWrite(9, HIGH);
  delay(500);
  digitalWrite(9, LOW);
  digitalWrite(10, HIGH);
  delay(500);
  digitalWrite(10, LOW);
  digitalWrite(11, HIGH);
  delay(500);
  digitalWrite(11, LOW);  
}


Some pins can write PWM signals, such as those used to control a servo.
These pins have the special ability to send a short "burst" of power and we can use this power to regulate the brightness of the LED.

These pins have PWM: 3, 5, 6, 9, 10, 11

The analogWrite function has nothing to do with the "6 analog" pins or the analogRead function.

https://www.arduino.cc/en/Reference/AnalogWrite


To control the LED with PWM, use the analogWrite function of Arduino to control the "brightness" of the LED color.
Each color will require a number from 0 to 255.

Turn LED on pin 9 fully on.
analogWrite(9, 255);

Turn LED on pin 9 to 50% brightness.
analogWrite(9, 127);

Turn LED on pin 9 to 25% brightness.
analogWrite(9, 63);

Turn LED on pin 9 fully off.
analogWrite(9, 0);


All values must be less than or equal to 255... such as 50, 100, 127, etc. and a positive integer, so no values less than ZERO.

This code example will cycle through each color for 1/2 second using the analogWrite function, instead of digitalWrite.
void setup() {
  pinMode(9, OUTPUT); //RED
  pinMode(10, OUTPUT); //GREEN
  pinMode(11, OUTPUT); //BLUE
}

void loop() {
  analogWrite(9, 255);
  delay(500);
  analogWrite(9, 0);
  analogWrite(10, 255);
  delay(500);
  analogWrite(10, 0);
  analogWrite(11, 255);
  delay(500);
  analogWrite(11, 0);  
}


This code example will fade one color of an RGB LED on and off continuously.
int LED = 9;

void setup() {
  pinMode(LED, OUTPUT);
}

void loop() {
  for(int Brightness = 0 ; Brightness <= 255; Brightness += 1) {
    analogWrite(LED, Brightness);
    delay(5);
  }

  for(int Brightness = 255 ; Brightness >= 0; Brightness -= 1) {
    analogWrite(LED, Brightness);
    delay(5);
  }
}

Try adjusting the LED number to switch LED, such as 9, 10 or 11.
Try adjusting the delay.
Try adjusting the "Brightness" increment value, such as: 1, 2, 5 or 10.

This code example cycles through all three RGB colors using "nested" FOR loops and array defining all three pins.
int LED[] = { 9, 10, 11 };

void setup() {
  for(int counter = 0; counter < 3; counter++) {
    pinMode(LED[counter], OUTPUT);
  }
}

void loop() {
  for(int counter = 0; counter < 3; counter++) {
    for(int Brightness = 0 ; Brightness <= 255; Brightness += 1) {
      analogWrite(LED[counter], Brightness);
      delay(5);
    }
  
    for(int Brightness = 255 ; Brightness >= 0; Brightness -= 1) {
      analogWrite(LED[counter], Brightness);
      delay(5);
    }
  }
}