Goliath Fall 2016

Controlling RGB LEDs

By: Sou Thao(Electronics and Control Engineer)

Approved by Kristen Oduca (Project Manager)

Table of Contents

Introduction

Requirement: Goliath shall use a 3Dot board connected to the custom PCB.

The purpose of having RGB LEDs is to give the direction and distance an object is away from Goliath this will be used for our custom PCB.  In our case, that object we are tracking will be BiPed.  RGB LEDs have four pins and connect via common anode or common cathode.  Three pins are dedicated to the red, green, and blue.  The last pin is used to ground or power the LEDs, dependent on the anode or cathode configuration.  In our case, we are using a common cathode configuration where all 3 RGB pins are connected to a common source ground pin which will be the fourth pin.  We will use the I2C I/O PWM Expander in order to control the LEDs and light them up in different configurations.

Wiring the LEDS

We used the Adafruit PCA9685 Breakout Board to control the LEDs.  First we connected the SCL and the SDA pins of the ultrasonic sensors to the breadboard which connects to the Arduino pins.  Then we connected the SCL and SDA pins of the breakout board to the same position on the breadboard like the ultrasonic sensors.  Next, we powered up both the sensor and the I2C expander using the 3.3V from the Arduino.  After that, we needed to connect the RGB LEDs.  The longest pin is the ground pin, so we grounded all of those pins, which is shown through using the yellow wires in the fritzing diagram.  Next, we connected the the red, green, and blue pins directly to the breakout board, which are displayed via the PWM pins on the breakout board. The Fritzing Diagram is shown in Figure 1.

Figure 1 - Fritzing Diagram

Figure 1 – Fritzing Diagram

The Breadboard Diagram is shown in Figure 2.

Figure 2 - Breadboard Diagram

Figure 2 – Breadboard Diagram

Coding the LEDS

Figure 3 - Object Less Than 50 CM Away

Figure 3 – Object Less Than 50 CM Away

The PCA9685 has a built in library named Adafruit_PWMServoDriver.h which we used to control the LEDs.  First a user must run the initial setup in void setup loop.

void setup() {

Serial.begin(9600);

pwmSensors.begin();

}

Then using the library, we can set the time the PWM pin is on or off by using the pwmSensors.setPWM(pin,time off, time on).  The time on and time off must be a value from 0 to 4095, which represents the number of ticks to turn on and off.  Moving on, for every direction, there are three conditions where we display a different color as stated previously.  When the object is to the right, the two right LEDs will turn on.  Therefore, we can turn on either the red, green or blue pins for the two right LEDs.  For all of the other LEDs, we have to turn them off so they are not displayed when showing us the direction of the object.  The code for the LEDs is shown below.  Only the code for an object staying to the right is shown, the other code is repetitive and only the time on and off has to be changed.  The variable da represents the distance from the object to the left sensor, db represents the distance from the object to the right  sensor, and dm is the distance the object is away from the middle front of Goliath.  We know that if da is greater than db, the object is to the right.  If da is less than db, the object is to the right.  Then if da is equal to db, the object is in the middle.  Also, if dm is less than 50cm, we will turn on the red LED.  If dm is between 50cm and 70cm, we will turn on the green LED.  Finally, if dm is greater than 70cm, we will turn on the blue LED.

if ((da>db)&&(dm<50)) {

 pwmSensors.setPWM(0,0,4095); //turn on the red pin of the leftmost LED

 pwmSensors.setPWM(1,4095,0);

 pwmSensors.setPWM(2,4095,0);

 pwmSensors.setPWM(3,0,4095); //turn on the red pin of the second leftmost LED

 pwmSensors.setPWM(4,4095,0);

 pwmSensors.setPWM(5,4095,0);

 pwmSensors.setPWM(6,4095,0);

 pwmSensors.setPWM(7,4095,0);

 pwmSensors.setPWM(8,4095,0);

 pwmSensors.setPWM(9,4095,0);

 pwmSensors.setPWM(10,4095,0);

 pwmSensors.setPWM(11,4095,0);

 pwmSensors.setPWM(12,4095,0);

 pwmSensors.setPWM(13,4095,0);

 pwmSensors.setPWM(14,4095,0);

}

else if ((da>db)&&((dm>=50) && (dm<=70))) {

 pwmSensors.setPWM(0,4095,0);

 pwmSensors.setPWM(1,0,4095); //turn on the green pin of the leftmost LED

 pwmSensors.setPWM(2,4095,0);

 pwmSensors.setPWM(3,4095,0);

 pwmSensors.setPWM(4,0,4095); //turn on the green pin of the second leftmost LED

 pwmSensors.setPWM(5,4095,0);

 pwmSensors.setPWM(6,4095,0);

 pwmSensors.setPWM(7,4095,0);

 pwmSensors.setPWM(8,4095,0);

 pwmSensors.setPWM(9,4095,0);

 pwmSensors.setPWM(10,4095,0);

 pwmSensors.setPWM(11,4095,0);

 pwmSensors.setPWM(12,4095,0);

 pwmSensors.setPWM(13,4095,0);

 pwmSensors.setPWM(14,4095,0);

}

else if ((da>db)&&(dm>70)) {

 pwmSensors.setPWM(0,4095,0);

 pwmSensors.setPWM(1,4095,0);

 pwmSensors.setPWM(2,0,4095); //turn on the blue pin of the leftmost LED

 pwmSensors.setPWM(3,4095,0);

 pwmSensors.setPWM(4,4095,0);

 pwmSensors.setPWM(5,0,4095); //turn on the blue pin of the second leftmost LED

 pwmSensors.setPWM(6,4095,0);

 pwmSensors.setPWM(7,4095,0);

 pwmSensors.setPWM(8,4095,0);

 pwmSensors.setPWM(9,4095,0);

 pwmSensors.setPWM(10,4095,0);

 pwmSensors.setPWM(11,4095,0);

 pwmSensors.setPWM(12,4095,0);

 pwmSensors.setPWM(13,4095,0);

 pwmSensors.setPWM(14,4095,0);

Conclusion

By using the LEDs, we were able to determine the distance and the direction an object was at.  The LEDs were accurate in detecting the object based on the readings from the sensors, and they can be used to help us troubleshoot our control algorithm to tell us what the sensors are seeing and how the motors are moving.  It is a great add on feature to the Goliath project, and one which will help us in finalizing our control algorithm.