In the first post of my blog I will explain how I created the pressing advertisement project seen below.
You will need:
- Cable (Approx. 30)
- Power ( Arudino-PC usb connection or 2 9V batteries)
- 2 Sharp GP2Y0A21YK0F Analog Distance Sensors
- DC Motor Driver
- DC Motor
The details will be shown in 3 different chapters.
In the first chapter I connected sensors to Arduino. The sensors that I used have 3 different outputs which are power (5volt), ground (gnd) and analog output. It generates analog values according to measured distance between sensor and the target. The numbers decrease if there is nothing in the front of the sensor and increase when something has been captured. Otherwise, there is a capture distance as well. It is between 30cm to 2 meters long in my sensor.
The sensor can be connected to the 5V output of Arduino if it is connected to a pc directly with USB connection. However you can feed Arduino by an external power supply as you can see in the example.
Analog output should be connected to the analog input of Arduino. They marked with an “A” before the number like A0 A1 A2 A3… For the codes that I shared, you need to connect the analog output to A0 pin on Arduino or you can just edit the codes.
If you have connected the wires as above, its time to copy the codes to Arduino software.
int sensor1 = 0;
int val = 0;
Serial.begin(9600); // setup serial
val = analogRead(sensor1); // read the input pin
Serial.println(val); // debug value
If you did the connections between arduino and sensors correctly, you should see the numbers flowing in serial monitor. (Toolbar>tools>serial monitor or Ctrl+shift+M)
Congrats, you succeeded the first part of the project.
In this chapter I will explain how to connect a dc motor via motor driver to Arduino. I used a common dc motor for the project. It has only two power outputs.
Driving the motor with arduino is not as simple as connecting the sensors. We need to use a motor driver. The driver that I use is Pololu md05a. Unfortunately it is out of production now but basically they all are the same.
Here is the schema;
We have 2 batteries here, but you can also use usb connection to Arduino. So you will not need to use the battery which is connected to Vin input on Arduino.
As you can see, motor driver is seperated by two sides. At the right side, OUT 1A, OUT 1B are connected to outputs of motor. This side is only for feeding the power needed by the motor. Vin and GND is connected directly to the battery. At the left side there are pins for making connection to Arduino.
1DIAG/EN is Enable pin. It needs to be connected to 5 volt. So it will be able to work.
1INa,1INb are logic inputs of motor driver. We use them for direction of spinning.
1PWM is for speed and torque of spinning.
Vin and GND are power and ground as we have seen before. Vin which is feeding 1INa, 1INb and 1PWM must be connected to 5 volt.
If all the connections are O.K. you can copy and paste the code below.
int IN1 = 2;
int IN2 = 4;
int D2 = 3;
If you see the motor is spinning its done. You can manipulate the code by changing the values of digitalWrite lines or changing the number of analogWrite line-100 is the speed of motor. You can increase or decrease it between 0-255-.
In the last chapter, we will complete the project by connecting previous chapters. Actually this chapter is more like logic background of the project. Lets have a look on the schema below.
This scheme shows the condition differences in individual scenerios.
In the first condition left side sensor finds a target, right sensor finds nothing. So the motor must spin to the direction of left sensor. It is the counter clockwise. According to second condition left sensor has nothing to seek, however right sensor has a new target. Motor must spin through the right sensor despite of the previous condition. In condition 3, both sensor find target. We can’t be sure about the targets movement and best thing to do is waiting until one of the sensors lose its target. This condition usually happens because of two or more people is on the area. Last condition happens if both sensors have no target. So there is nothing to worry about.
If its clear we can start to prepare Arudino for the project.
The picture above leads us to make connections for the last time. It is the same as the examples we have seen before. I will not repeat the steps, we can go directly to the codes.
I hope you like this tutorial. See you until next time.