Follow Up Advertising How To

Introduction

In the first post of my blog I will explain how I created the pressing advertisement project seen below.

Follow up Advertising from gunisigicihangir on Vimeo.

You will need:

  • Arduino
  • Cable (Approx. 30)
  • Power ( Arudino-PC usb connection or 2 9V batteries)
  • 2 Sharp GP2Y0A21YK0F Analog Distance Sensors
  • DC Motor Driver
  • DC Motor
  • Breadboard

The details will be shown in 3 different chapters.

Chapter 1

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.

sensor-connection

If you have connected the wires as above, its time to copy the codes to Arduino software.

int sensor1 = 0;
int val = 0;

void setup()
{

Serial.begin(9600); // setup serial

}
void loop()
{
val = analogRead(sensor1); // read the input pin
Serial.println(val); // debug value
}

serial monitor sensors

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.

 

 

 

Chapter 2

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;

motor-connection

 

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;

void setup()
{

pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(D2, OUTPUT);
analogWrite(D2, 100);
}
void loop()
{
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
}

 

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-.

Chapter 3

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.

sensors-01

 

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.

sensor+motor-connection

 

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.

int sensor1 = 0;
int sensor2 = 1;
int val = 0;
int val2 = 0;
int IN1 = 2;
int IN2 = 4;
int D2 = 3;
void setup(){
 Serial.begin(9600);
 pinMode(IN1, OUTPUT);
 pinMode(IN2, OUTPUT);
 pinMode(D2, OUTPUT);
  analogWrite(D2, 25);
 }
void loop(){
 val = analogRead(sensor1);
 val2 = analogRead(sensor2);
 if ((val > 100)&&(val2<100)) {
  digitalWrite(IN1, LOW);
 digitalWrite(IN2, HIGH);
 }
 else if ((val < 100) && (val2>100)) {
  digitalWrite(IN1, HIGH);
 digitalWrite(IN2, LOW);
 }
 else {
  digitalWrite(IN1, LOW);
 digitalWrite(IN2, LOW);
 }
 Serial.println(val2);
 delay(100);
}
I added only the “if statement” to the codes. It performs the conditions which is in the first schema of the chapter. It runs like “if val(sensor1) higher than 100 and the val2(sensor2) is lower than 100 spin clockwise or if val(sensor1) is lower than 100 and val2(sensor2) is higher than 100 spin counter clockwise, else, do nothing.)

 

I hope you like this tutorial. See you until next time.

Advertisements