Home

Output devices

So, here we are, the last lesson before the final project.

An output device is the opposite of a sensor; it converts back the electrical signal to a physical motion.

There are many types of output devices: LEDs, LCDs, speakers, motors and many more.

In this lesson I will control an RGB LED using the measurements we had from the ultrasonic sensor.

RGB LED

An RGB LED is an LED that consists of three colors, Red, Green and Blue. It can show any of these three colors or any other color that exists, you only need to control the percentage of each color in the mix. Here is the pinout of such an LED:

I have to mention that there are two types of RGB LEDs:

  1. The common cathod in which you connect the three color pins to the digital pins, high voltage, and the three pins share the same ground.
  2. On the contrary ,the common anode in which the three pins share the same high voltage, but each one has a separate ground pin.

Here I will be using the common cathod type, it is more common.

Connections

In this circuit I am using the setup from the previous lesson plus adding the four pins of the RGB LED.

It is important to mention that whenever you connect an LED you should connect it with a resistor, otherwise the current will burn it. Normally 330 ohms resistor is used.

Programming

Again, here in the beginning I am declaring the variables for the pins and the measurements.

I would like to remind you to always comment your code, that way it is even self explainatory. It is very helpful more than you'd ever thought.

In the void setup section I am declaring the pins modes, all getting a high voltage except the Echo pin.

In this section of the void loop I am sending a pulse and then listening to it, this is how the ultrasonic sensor works. Then I am doing some calculations and reading them through the serial monitor.

In this piece of code I am controlling which color would the light have depending on the distance from the sensor. This control is divided into three seciotns:

  1. In the first section, from 0 to 10 cm far from the sensor, the LED is to have a red color which gets stronger the further you are from the sensor.
  2. In the second section, from 10 to 20 cm far from the sensor, the LED is to have a green color which gets stronger the further you are from the sensor.
  3. In the third section, from 20 to 30 cm far from the sensor, the LED is to have a blue color which gets stronger the further you are from the sensor.
  4. The last case is when the object gets out of the 30 cm zone, then the LED will turn off.

Readings

Here is the control of the RGB LEDt based on the distance from the sensor