Distance Motion Sensor With Logic AND Gate

by 630963 in Circuits > Arduino

627 Views, 2 Favorites, 0 Comments

Distance Motion Sensor With Logic AND Gate

Screen Shot 2021-06-23 at 9.31.55 AM.png

What Is This Project About?

This project demonstrates a perfect example of using a seven-segment display with the help of an RGB and a distance sensor. As the object is coming closer to the sensor the RGB will display three colors, either green, yellow, and red. Green represents stage 3, yellow represents stage 2, and red represents stage 1 which means that the object is very close to the sensor. At the same time, the project will be using a logic gate that'll allow you to turn off the components when both of the slide switches are turned on. The purpose of this is to demonstrate that using logic gates is useful when you want to interact with the circuit.

What Did I Learn This Semester?

I'm very grateful for this semester that I have had. This specific engineering course was one of my favorite courses that I had in a while. There were some things that were repeated or taught again since I took this course in grade 10, but a lot of new things were taught to me as well. For example, how we use 2-bit adders, how to simplify a truth table and create a schematic out of it, and how to use different microcontrollers such as the distance sensor, seven-segment display, push buttons, 555 timer, and different types of logic gates.

What Inspired Me to Build This Project?

I have always found distance sensors and segment displays to be interesting. My goal was to use a logic gate that would control the on and off situation on my circuit. Meaning that if both slide switches are turned on all of the components except the sensor turn off, and when the slide switches are turned off or one of them is turned off all of the components are ready to be used.

Gather Components

Screen Shot 2021-06-22 at 9.07.58 AM.png

Now you know the purpose of this project it's time to gather all of the components! The first thing you should do is open up tinkercad and go to the search bar. On the search bar, you'll be able to choose different types of components.

Components Needed:

1 x arduino

2 x slide switches

6 x 1k resistors

1 x AND logic gate

1 x ultrasonic distance sensor(3 pins)

1 x 7 segment display

1 x buzzer

1 x breadboard

The components that have been listed above are the components that you'll need in order to build this circuit.

Cost Breakdown

Although this circuit can be built on tinkercad for free, you may want to build this circuit with actual components sometime in the future. So I'll do a cost breakdown on where you can get these components, and how much they cost.

Circuit Beginner - $35.99

AND Logic Gate - $0.86

Total Price without Tax: $36.85

Price Including Tax: $41.64

Build

Screen Shot 2021-06-22 at 12.08.53 AM.png
Screen Shot 2021-06-22 at 12.15.43 AM.png
Screen Shot 2021-06-22 at 12.44.24 AM.png

Yay! You have finally made it to the part where you get to build the circuit, congrats! Now it's time to build!

Step 1:

The first thing you want to do is place all of the components onto the breadboard. Attach the seven-segment display, slide switches, and the AND gate onto the breadboard.

Step 2:

Once you have attached these components to the breadboard you'll need to add the resistors. The purpose of the resistors is to make sure that extra current is not going through the components otherwise the components will not work efficiently. Attach two resistors to the power terminal on the seven-segment display. The reason why we are doing this is that we want the display to be a common anode, not a common cathode. Once you have attached the resistors to the display you'll want to connect the ultrasonic distance sensor on the breadboard.

As you can see there are 3 pins on the sensor. Each pin has its own purpose. The far-left pin is the ground pin. Meaning it'll be connected to the negative side of the breadboard. The middle pin is the power pin which means it belongs to the positive side of the breadboard. The final and last pin which is on the right is the signal pin. The purpose of this pin is to give a signal towards the sensor to pick up objects that either move away or come towards the sensor. For now, connect the signal pin to one of the pins on the breadboard. This pin will later be extended with a jumper wire connection onto the arduino.

Connecting the Logic Gate and Buzzer

Screen Shot 2021-06-22 at 8.33.11 AM.png
Screen Shot 2021-06-22 at 8.44.16 AM.png

Since you have connected the distance sensor and the seven-segment display it's time to attach the buzzer and the logic gate. Now for the logic gate there a few things that you must do. First off is to connect the ground terminal to the ground pin and the power terminal to power. Make sure you color-code your jumper wires. It is extremely important to color code your wires as it'll demonstrate your learning when you are connecting your wires. Red represents the power line while black represents the ground line. The first image attach should tell you how you should have connected the pins to the appropriate pins on the breadboard.

Now you connected the ground and power pin the next step is to connect the buzzer. On the buzzer, you should see 2 sides. One side must say positive while the other side says negative. Now since we want the current through the power pin we will connect the buzzer's power railing to any of the pins on the breadboard except the power railing as we will do a jumper connection on the arduino, and as said before make sure to connect the ground railing to the ground pin on the breadboard. The second image shows you how the connection must be.

Slide Switches and Resistors

Screen Shot 2021-06-22 at 10.42.12 AM.png

Now the final connection to the breadboard is the slide switch and the resistors. On the slide switch, there are two terminals, terminal 1 and terminal 2. It doesn't matter on what side where you place ground or power as both sides have the same purpose. Now where you place the power and ground is pretty important. For example, I wired terminal 1 as ground and terminal 2 as power. So, my terminal 1 is considered the offside while terminal 2 is my on side since the power railing is connected to power on terminal 2. Once the slide switches have been declared on what side is on and off it is time to add the resistors. The purpose of the resistors is to reduce the current that'll be coming from the arduino so the components won't heat or cause a short circuit. Now, where you connected the buzzer's power railing on the breadboard is where you place a resistor. The resistor should be connected right underneath the power connection from the buzzer. Once the resistor is connected we will do a jumper connection onto the ardunio in our next step.

Now you will want to connect the rest of the 3 resistors to the pins of the RGB. So connect one resistor to the red pin, one resistor to the red pin, the second resistor to the blue pin, and then finally the last resistor to the green pin. The image provided for this step should exactly look like the circuit that you have created.

Connecting the Seven Segment Display to the Arduino

Screen Shot 2021-06-22 at 12.17.50 PM.png

Now since all of the components have been placed on the breadboard and have been connected to their appropriate pins such as ground and power, it is time to connect the components onto the arduino. Now, this part may be complicated so make sure you look at these steps carefully. The first component that we will wire will be the seven-segment display.

pin g = 11

pin f = 10

pin a = 9

pin b = 8

pin c = 5

pin d = 6

pin e = 7

You might be wondering why aren't we coding the pin "DP". DP stands for decimal point, and since we will not be using any decimal points there is no need for us to use it at all so therefore we will not be wiring that particular pin.

The pins that I have just mentioned are the pins that I connected each terminal of the segment display onto the arduino. Using the same exact pins as I used is not necessary as you can use any digital pin, but remember what pin you have connected your components to as it'll be needed when we get to the coding part of this tutorial. Remember that the pins that I have just mentioned are digital pins which you will see on the right side of the arduino. The image that I have provided should be quite similar to your circuit.

Now we will wire the buzzer and the sensor onto the ardunio.

Connecting the Buzzer and Ultrasonic Distance Sensor

Screen Shot 2021-06-23 at 8.07.11 AM.png

Our next step is to connect the buzzer and the ultrasonic sensor onto the arduino. The same process will apply again here as well. The first component we will connect is the distance sensor. This is the same process as before. We will create a designated pin for the sensor.

The first component that we will connect will be the sensor. Remember in the beginning that the signal terminal will be connected to the ardunio, well now it's time to connect it. In the beginning, we connected the signal terminal onto the breadboard so now we will add a jumper connection from that row to the arduino. The pin that I had used to connect the sensor is pin number 12. Again, it doesn't matter what digital pin you use as long as you know which one is connected to what. This will be useful when we are doing the coding of the circuit.

Now the sensor has been connected, it is time to connect the buzzer. The same process begins here as well. We will do a jumper connection from the pin that the buzzer was connected to on the breadboard. The pin that I had connected the buzzer to is A0. Remember that an analog pin can act just as a digital pin.

The image that I have attached above should look very similar to the circuit that you had built on tinkercad.

Connecting the Slide Switches to the Logic Gate

Screen Shot 2021-06-23 at 9.03.18 AM.png
two-input-and-gate-truth-table.jpeg

The logic gate that we are using is called a AND gate. These AND gates have a special purpose in our circuit. When both of the slide switches are turned on the AND gate sends a signal to the ardunio that makes the buzzer, RGB color, and the seven-segment display turn off until one of the switches is turned back off. The reason for this is because the only time a AND gate is able to produce an output of 1 which represents a HIGH when both of the inputs are 1. So for the AND gate to send a signal to the arduino both of the slide switches must be turned on.

If you were to look at the truth table of a AND gate you can see why both switches have to be on. The image above shows a truth table of a AND gate. As you can see that the only time the output is 1 is when both of the switches are turned on not off, so meaning if one of the switches is turned on the output, will be 0. The only time it's 1 when both switches are turned on.

We already wired the logic gate and the slide switches in our previous steps so the only part is to connect them together. There should be a pin that is called a common pin that has not been connected to anything. So we will take the common pin and make a jumper connection to the gate. On the AND gate there are multiple pins. The pin input 1A and input 1B will be the pins that we will use. Both inputs 1A and 1B are our slide switches. So as said connect both the common pins to 1A and 1B. Make sure you change the colors of both the inputs as it looks neater by using different colors rather than using 1 color each time. Once both your slide switches have been connected we will use the next pin which is called the "Output 1" and connect it to pin number 13. Again, it doesn't matter what pin you use as long as you know on what pin each of the components is connected to. We will use the pins when we get to the coding part of this project.

The image that I have attached should look quite similar to the circuit that you have built so far.

Connecting the RGB to the Ardunio

Screen Shot 2021-06-23 at 9.37.43 AM.png

The last component that is left to connect is the RGB. This component is fairly easy to connect as the same process of connecting the other components. We will create a jumper wire connection from each of the pin which was the red, green, and blue pins onto the arduino. Again, it doesn't matter what pin you attach it to on the arduino. This could be a digital or an analog pin as both can serve the same purpose as input/output pins.

Here are the pins to which I connected my terminals:

Red - 4

Blue - 3

Green - 2

Now it might be nice to color code this section. What I mean by this is when connecting the green pin to the arduino make the jumper wire color green, and the blue pin blue, but for the red one just keep it as orange. The reason why we won't be using a red color jumper wire as it represents a power signal. So just color code the green, and blue wires.

Anything so far in the project that we have connected to the arduino will be the pins that'll be used in the software component of the project. The image that has been attached to this step is my circuit so far. The circuit that I have should be quite similar to your circuit as the process for both of us is the same.

Connecting Ground and Power

Screen Shot 2021-06-23 at 9.48.53 AM.png

The last and final step of this circuit is to give ground and power to the breadboard. Now, on the arduino there should be a pin called GND, and another pin called 5V. GND represents ground while 5V represents power. In order for the breadboard to have both ground and power, we must connect the pins to it.

Now since our components are using both ground and power on each side of the breadboard we must have a jumper connection of both ground and power from one end to the other. So I connected ground on one side of the breadboard and power to the other side. The image above will give you a better understanding of what I mean by that. Also, remember to always color code your wires when dealing with ground and power. Ground is black and power is red.

As you can see from the image above that I connected ground to one side of the breadboard and power to the other side. Since ground and power are only be provided to one side of the breadboard we must have a jumper connection. As you can see at the end of the breadboard that a jumper wire connection has been placed to send power and ground to the opposite side of the breadboard. This will allow ground and power to pass through on both sides.

Code!

Screen Shot 2021-06-23 at 10.14.47 AM.png

Yay! Congratulations, you have finally made it to the coding part of this project. Remember when I asked you to know which pin you connected each of the components to? Well, now you will need it. In the code, we have to specifically specify to which pin each component is connected.

Now, in order to code, you must click the code section on the tinkercad. When you click it you will default to the block method. In order to get the text method just click the little icon next to the block and you will see the option of text. Click text, and then you will be taken to the text method. In this method, there will be code already written down so just delete whatever has been coded. We will start fresh.

The first thing we will do is declare the pins that we have connected the seven-segment display to.

//Declaring pins for 7 Segment Display
int g = 11;

int f = 10;

int a = 9;

int b = 8;

int c = 5;

int d = 6;

int e = 7;

The "int" represents an integer value. int allows you to define numeric values, and since the segment display is being connected to digital pins we must use int otherwise the compiler won't know what to do with g, f, a, and the other pins. By stating that it is an integer it allows the compiler to hold a certain value for them. Since the segment display is a common anode in our code we will write down "LOW" to turn it on. When it is turn on the LOW represents an integer value of 0. That 0 will be carried to the arduino which will then allow that specific part of the arduino to turn on. More of this will be talked about when we get to that specific part of the code.

Remember to always add comments to your code as it makes it easier to read for others. To add comments just type "//". Now, the semicolon represents end statements. Whenever you are done with a particular line you will write down a semicolon. This will allow you to go to the next line. The image that I have provided is the code that I have.

Declaring RGB, Sensor, Buzzer, and Logic Gate

Screen Shot 2021-06-23 at 10.42.49 AM.png

This step will be quite long so be ready! We will now declare the pins for the RGB LED, sensor, buzzer, and logic gate.

//Declaring pins for RGB

int redPin = 4;

int greenPin = 3;

int bluePin = 2;

//Pin number for sensor

const int pingPin = 12;

//pin number for logic gate

int logicoff = 13;

//pin number for buzzer

int buzz = A0;

The same process applies here as well. I wrote down a comment stating that I'll be declaring the pins for the RGB, sensor, logic gate, and buzzer. I stated that the RGB pins, the sensor pin, and the logic gate pins are int values so there will be a numeric value given to the components. Again you can see that I have stated to which pin each of the components has been connected to, and at the end when I'm done with that particular line I add a semicolon to move onto the next line. If you forget to add a semicolon there will be an error shown when you run to simulate the program.

Now for the logic gate, I called the variable as logic off. Since the output of the logic gate is being connected to pin number 13 I wrote int logicoff = 13;.

Before we go on further we will declare two more variables. Inches and duration. Instead of int, we will use long. The reason for this is because long will allow us to store long 64-bit integers.

long duration, inches;

Void Setup()

Screen Shot 2021-06-23 at 11.40.26 AM.png

Throughout the code, this is the most important part. The void setup is the part of the code where you will declare your inputs and outputs. The first thing you will do is write a void setup like this "void setup()". When you are done writing this done you will press enter and add a bracket that looks like this "{". This is where you will write down the code of your inputs and output.

void setup()

{

//declaring inputs and
pinMode(redPin, OUTPUT);

pinMode(bluePin, OUTPUT);

pinMode(greenPin, OUTPUT);

pinMode(buzz, OUTPUT);

pinMode(logicoff, INPUT);

Serial.begin(9600);

pinMode(g, OUTPUT);

pinMode(f, OUTPUT);

pinMode(a, OUTPUT);

pinMode(b, OUTPUT);

pinMode(c, OUTPUT);

pinMode(d, OUTPUT);

pinMode(e, OUTPUT);

}

What is pinMode?

pinMode is a very important unique function. The purpose of this function is to allow the components that you have connected to the arduino to act as an input or an output. So when you write pinMode, you add parenthesis and state what component you are stating as an input or an output. For example, I said that the buzzer will be output. Since I stated that the buzzer is buzz I wrote pinMode(buzz, OUTPUT);. This process repeats again for the other components as well.

As you can see from the code above that I have stated that the pins on the seven segment display are output pins since they will be stating a number such as 3, 2, and 1. I also stated that the pin for the RGB are output as well since they will be displaying a particular color such as green, yellow, and red. The buzzer is also an output pin since it'll buzz when the inches of the sensor are less than 10 inches. Now the only difference you will see that that the logicoff will be an input value. The reason for this is because the logic gate has no purpose to output a value. When both of the slide switches are turned on the value of 1 will pass through the jumper wire that is being connected and turn off all of the components on the breadboard. So therefore I stated that the logicoff variable is an input, not an output.

Also, you may have noticed that the output and input are in capitals. In C, you must state these inputs and outputs in all caps.

Void Loop()

Screen Shot 2021-06-23 at 12.28.16 PM.png

The next step of the code is to do the void loop(). This is also another important function of the code as well. Whatever will be coded inside of the void loop will run over and over again until the maker board is turned off, meaning when you turn off the simulation. So the first thing you will write will be void loop(). The same thing as void setup(). Once you are done that press the enter key and add the same bracket "{". Now, this where the complicated stuff will happen.

One thing we know is that when both of the slide switches are turned on the value of 1 will be passed to the ardunio and all of the components except the distance sensor will be turned off. So in order for this to happen, we will use a while loop. A while loop will help us use all of the components as long as the slide switches are turned off or in other words when the output of the logic gate is 0. The minute the logic gate outputs a 1 meaning both the slide switches are turned on the compiler will exit the loop and read the rest of the code that will be written.

So write this:

while(digitalRead(logicoff) ==0){
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);

Inside the while loop, I wrote down digitalRead(logicoff)==0. digitalRead is a function that reads an incoming value as high or low. 1 representing high, and 0 representing low. So as long as both of the slide switches are turned off the while loop will run. The second both switches are turned on the compiler will exit the code.

Now, the first thing that I did was pinmode the sensor as we didn't do it inside of void setup(). Now the rest of the code might look confusing to you so I'm going to explain. digitalWrite lets us set the components to HIGH or LOW. Meaning either it outputs a 1 or a 0. 1 being on, and 0 being off. For the sensor, I stated at first the sensor will act as an output meaning it'll collect the data, and then it'll act as an input meaning the data will be transferred to the arduino so the RGB colors and buzzer can respond to the data collected. The delay means waiting for a couple of seconds before the next line can be executed. Instead of seconds, I used microseconds.

If Statements

Screen Shot 2021-06-23 at 12.29.14 PM.png
Screen Shot 2021-06-23 at 12.29.23 PM.png

The last part of the while loop is to add if statements. The purpose of these if statements are to execute the RGB colors, and output the numbers from the display. For example, in some certain range the color green list up or yellow, and even red, and then the corresponding number will be output. For the first if-statement, I stated that if the inches are greater than or equal to 100 the color green is outputted. So I coded the hex number of green. I also stated that the buzzer is at low meaning that it won't buzz. The reason for this is because when the inches are less than 10 the buzzer will buzz, but when I go over 10 the buzzer won't stop buzzing. This is due to the code that we wrote. Since we didn't state that the buzzer should stop buzzing once over 10 inches it'll continue to buzz until you turn off the program. That is why in each situation we wrote down that the buzzer must be at the state of low until it goes under 10 inches.

This "&&" means and. So when typing down my code I had to write two conditions. This allowed me to have a range between two values.

Also, you can see that I wrote down the inputs of the seven-segment display as high or low. Now, since the display is a common anode in order to turn on a specific part of the display we must write "LOW" instead of "HIGH". If the display was a common cathode we would have coded the opposite. Since I want to display the number 3 I wrote down specific pins that have to be turned on and off in order for that specific number to appear.

Now we move on to the next part of the code. We will be using else if statements since we want the if statement to be connected to one another. The same process repeats. I wrote the range to which the inches should be and displayed the color yellow, and coded what pins on the display should be high or low to display the number 2.

The same process happens again, I coded the range to which the color red should be outputted and then coded what has to be high or low to output 1. The last if statement was when the sensor goes underneath 10 inches. Once under 10 inches the buzzer buzzes so I wrote the buzzer as HIGH.

Last Bit of Code

Screen Shot 2021-06-23 at 12.43.28 PM.png

The last part of the code is pretty simple. We will end the while loop by adding the bracket. Once we are done we will write the code to when both of the slide switches are turned on. As said before, the display is a common anode so in order to turn every pin off, we must state it as HIGH. So when both of the slide switches are turned on, all of the pins on the segment display are HIGH, the buzzer is LOW, the pins on the RGB are also low. The only component that is left to work is the sensor. Underneath where it says print inches is the code where the inches are printed on the serial monitor.

The last bit of code is just code that converts the microseconds into inches which then returns a value by using the return function. The last function that I created is a void function that allows me to set the color for each of the if statements.

Final Product

Screen Shot 2021-06-23 at 1.53.04 PM.png
Screen Shot 2021-06-23 at 1.35.00 PM.png

The image above should be the final product of your circuit. As said multiple times the circuit that I create should be somewhat or exactly similar to your circuit. The pins may be different if you choose to use different pins, but otherwise, there shouldn't be a significant difference between my circuit and yours.

The second image that I have attached is the image of the schematic. I believe a schematic is very important as it allows you to understand how everything is connected to each other. It may look complicated at first, but I encourage you to see your circuit and then compare it to the schematic provided. You may see that the wiring is the exact same since a schematic is usually based on the circuit that is created.

The sensor calculates how far the object is away, and then prints it on the serial monitor. Based on the distance the corresponding number displays. For example, if the inches are greater than or equal to 100 the color green displays, and the number 3 is being displayed. This process is true throughout the other parts of the code as well.