## Introduction: Ultrasonic Sensor Using RGB and 7 Segment Display

What Is the Purpose of This?

The purpose of this project is to use a distance sensor while a corresponding colour is lighting up. The seven segment display is used to show at what stage is the object is located. These numbers work till 1 to 3. 1 is a danger zone while 3 is a safe zone. This project will take time so you have to read this intractable carefully in order to understand the code, and the wiring.

As you can see from the images above, as the distance decrease the number on the seven segment display decreases and the RGB colour turns yellow and then red. If the distance is somewhere in the middle the number is 2 while the colour of the RGB is yellow, and if it is very close to the distance sensor the colour is red.

This is an amazing project to conduct since it teaches you how ultrasonic sensors work in order to track an object.

## Step 1: Materials

Here is an image of what materials are needed.

First Picture:

Arduino(1x)

Second Picture:

Jumper Wires (20x)

Third Picture:

Seven Segment Display(3x)

Fourth Picture:

Ultrasonic Distance Sensor(1x)

Fifth Picture:

Resistor(5x)

Sixth Picture:

RGB Light(1x)

Make sure you have these materials in order to complete this project.

## Step 2: How Does the Seven Segment Display Work?

Here is a some information of how the Seven Segment Display works.

As you can see from the image uploaded, there are individual pins that connects to a certain alphabet. When you code this, you make a certain number light up by using the alphabets. Each alphabet is connected to a pin on the Arduino. I will cover this up when we are coding this project, but for now understand how this works.

There are two types of segment displays. Common Cathode(CC) and Common Anode(CA) The one we are using is a Common Anode(CA). The difference between Common Cathode and Common Anode is that Common Cathode has all the negative sides of the 7 segments directly connected together while the Common Anode has all the anodes directly connected together.

Here is a good link that clearly describes how Common Cathodes and Common Anode work.

Difference Between Common Cathode and Common Anode

## Step 3: How Does the RGB Light Work?

Again, it is important to understand how each component of this project works. Without having background knowledge, it'll be hard to complete this project.

If don't know this, RGB stands for the three primary colours on a RGB light; red, green and blue. With a RGB led light you can create any colour you wish with certain colour codes. For example, the colour code for purple is 128,0,128. By stating this code you are going to turn the RGB led light purple. You can also see the different colour codes on the image attached.

A RGB led light has four pins. Depending on what type of RGB you have. Usually, the two types of RGB lights are Common Cathode and Common Anode. The one we will use today will be a Common Cathode meaning one of its component will be connected to ground.

## Step 4: Ultrasonic Distance Sensor

This is one of the most important component in this project. Ultrasonic Distance Sensor. Distance sensors play role by calculating how far a specific object is. This sensor has three main components; ground, power and a signal which will be connected to any of the pins on the Arduino that has a number. The most obvious part is that the component that states ground will be connected to ground(GND), and power will be connected to power(5v).

If you don't know which pin is which, you can see by the image attached that each pin is labeled. For example the first the first pin is GND, the middle pin is power and the last pin is signal which would be connected to any of the pins that has a number on the Arduino.

The code will be explained later through this presentation.

## Step 5: Attaching the RGB and Seven Segment Display

Your first step is to attach the 7 segment display and the RGB light onto the breadboard. Once this is done you will attach three resistors onto the RGB light. Each resistor should be attached to one of the primary colours which is red, green and blue. The reason for this is because too much current can destroy the electronic. In order to have less current we will use a 1kΩ resistor. This will limit the current going through.

Once you have attached all of the resistors you will take a black wire and extend it to ground from the cathode pin on the RGB light. The reason for this is because we are using a common cathode RGB.

Now we will attach each of the RGB colours to a certain number on the Arduino.

Make sure you use black for ground and red for power. Also, try to use other colours when attaching different electronic components so you don't get confused.

## Step 6: Attaching the RGB and Seven Segment Display(pt.2)

Connect red onto pin 11, the cathode should be connected to ground of the breadboard, blue on pin 10 and green in pin 9. Once this is done we are ready to attach the seven segment display onto the Arduino as well.

Now, you have to make sure where you attach each alphabet to on the Arduino because we will need to code it once we are done the wiring. Here are the pins at which each alphabet will be connected to.

G - 13

F - 12

Common - Positive side

A - 8

B - 7

C - 6

D - 5

You might be wondering what pin does DP go into. We will not be using this pin, but if your interested what it does, it makes a decimal point beside the number. Now, you are done connecting each of the pins onto the Arduino. The only component left is connecting the Ultrasonic Distance Sensor. This part is pretty easy. All you have to do is connect the GND component to ground on the Arduino, the 5v to 5v on the Arduino and signal into pin 3.

Once this done you must give ground and power to the breadboard. So connect GND and 5v from the Arduino to the positive and negative terminals of the breadboard. Also, since you are connecting them to only one side of the breadboard you must attach a wire connecting both GND's and 5v's across each other since the seven segment display is using both of these railings. With this wire, we are giving ground and power across the breadboard. Look at the image attached.

## Step 7: Coding

This is the difficult part, make sure you fully understand the code and why it's being used. It's fairly simple but complicated at the same time since we will be writing a lot of stuff.

Step 1:

Your first step as always should be to declare your variables and the pins you have used on the Arduino. So on top of void setup() we will declare our global variables. So first we will state all of our pins for the seven segment display.

//Declaring pins for 7 Segment Display

int g = 13;
int f = 12;

int a = 8;

int b = 7;

int c = 6;

int d = 5;

int e = 2;

You might be wondering what is the purpose of "int". Int represents Integer. Meaning the wires we are plugging into are numbers and numbers are integers. So we are declaring that pin g is a integer value connecting to pin 13, and so on. Also, it's a good idea to write down comments. Comments allow us to clearly state what is the purpose of this code so when we are looking through it in the future or someone else is looking through it, they know what is going on. In order to add a comment, type two dashes(//) and then write whatever you want. So on the top of the code here I'm writing down that these pins represents numbers that is connected to the seven segment display.

Step 2:

Now we will declare the pins that we have attached to the RGB. The same principle follows. We will use int and declare the pins being used.

//Declaring pins for RGB

int redPin = 11;

int greenPin = 10;

int bluePin = 9;

Step 3:

Once this is done, it's time to declare the Ultrasonic Distance Sensor. We will use a term called "const". This is a short term for Constant. Const pretty much means that the variable is a read-only variable.

//Pin number for sensor

const int pingPin = 3;

We are still using int as well since 3 is a integer value. Now we are done declaring our global variables. It's time to get to the real coding. Also, don't forget to add comments to clarify the purpose of this code.

## Step 8: Continue of Coding

Now you have to go to a section void setup(). Once you are there go between the brackets and this is where you will write down the code. This part is very important because this is where you will state if each pin is a INPUT value of OUTPUT value. Since we are already giving specific pins to each of the electronic components involved, this will be a OUTPUT value.

pinMode(redPin, OUTPUT);
pinMode(bluePin, OUTPUT);

pinMode(greenPin, OUTPUT);

Serial.begin(9600);

pinMode(g, OUTPUT);

pinMode(f, OUTPUT);

pinMode(a, OUTPUT);

pinMode(b, OUTPUT);

pinMode(c, OUTPUT);

pinMode(d, OUTPUT);

pinMode(e, OUTPUT);

pinMode means that you want a specific pin in your circuit to behave either as a OUTPUT value or a INPUT value. In this project, we will use a INPUT value. So you are going to state which variable you want to use and write down that this pin is a OUTPUT value. The image attached shows how your code should be right now. If your wondering what does Serial.begin means, it's used to tell at what speed communication is going to be at between the circuit and Arduino. In this case, we are using 9600.

## Step 9: Void Loop() and VoidsetColor(int Red, Int Blue, Int Green)

This is where the main code will be. Void Loop() is where the code runs continuously since the function itself is called loop.

// in inches:

long duration, inches;

Long pretty much means that the data being processed will be long since the distance of the object will change all the time.

pinMode(pingPin, OUTPUT);

digitalWrite(pingPin, LOW);

delayMicroseconds(2);

digitalWrite(pingPin, HIGH);

delayMicroseconds(5);

digitalWrite(pingPin, LOW);

pinMode(pingPin, INPUT);

duration = pulseIn(pingPin, HIGH);

// convert time into a distance

inches = microsecondsToInches(duration)

Now, the PING is triggered by a HIGH pulse of 2 or even more microseconds. Once the pulse duration is over we will give it a LOW pulse so the next run will be fresh with accurate results. Since we want to know how many inches is the object we will convert it, so thats what the last part of the code means.

Before we go any further we have to write a void function beneath void loop(). The purpose of this code is that we can change the colour of the RGB to any colour we want. With out this, the only colours available would be green, blue and red.

Make sure when you are typing down the code you are typing between the two brackets. The reason for this is because that specific code belongs for that specific function only.

void setColor(int red, int blue, int green)
{

analogWrite(redPin, red);

analogWrite(greenPin, green);

analogWrite(bluePin, blue);

}

analogWrite means a PWM to a certain pin. PWM stands for Pulse Width Modulation. If you want to learn more about this, go to this website. This website talks more in dept of what PWM does and how it works.

Once this is over we will write down "if" statements inside Void loop(). The purpose of this is to write down each condition and state what colour will be lit and what number will be shown.

if(inches >= 100){

setColor(0,128,0);//Green

//Represents the number 3 when the inches are greater or equal to 100

setColor(0,128,0); //Green

digitalWrite(a,LOW);

digitalWrite(b,LOW);

digitalWrite(g,LOW);

digitalWrite(c,LOW);

digitalWrite(f,HIGH);

digitalWrite(d,LOW);

digitalWrite(e,HIGH);

}

This chunk of code means that if the inches from the distance sensor is greater than or equal to 100, the RGB light will be green and the number 3 will be shown on top of the seven segment display. LOW means its being on, and HIGH means it off. We will write down two more conditions for this circuit.

if(inches < 100){

setColor(255,255,0);//yellow

//This code represents the number 2

digitalWrite(a,LOW);

digitalWrite(b,LOW);

digitalWrite(g,LOW);

digitalWrite(c,HIGH);

digitalWrite(f,HIGH);

digitalWrite(d,LOW);

digitalWrite(e,LOW);

}

If the inches are less than 100, the RGB will display the colour yellow and the seven segment display will show the number 2.

if(inches < 50){

setColor(255,0,0);//Red

//This code represents the number 1

digitalWrite(a,HIGH);

digitalWrite(b,LOW);

digitalWrite(g,HIGH);

digitalWrite(c,LOW);

digitalWrite(f,HIGH);

digitalWrite(d,HIGH);

digitalWrite(e,HIGH);

}

If the inches are less than 50, the colour red will be lit and the number 1 will be shown.

Here is the last bit of code that is left before we are ready to launch this.

//Prints inches
Serial.print(inches);

Serial.print("in, ");

delay(100); }

long microsecondsToInches(long microseconds) {

return microseconds / 74 / 2;

}

This part pretty much means that it is converting the microseconds into inches.

## Step 10: Check If Your Circuit Works

The file attach shows how the code should be once you are done. Also, here is a video of how the circuit should work once everything is done.