We at Jaycon Systems have found yet another awesome project, and we are going to share it with you! Follow along as we guide you through the process of buiding your own IR Radar Alarm. In this tutorial we are going to show you how to assemble an IR radar that covers a field of 360 degrees. The radar includes an LED and a buzzer to indicate when the radar has detected the presence of an object.

What is IR?

IR stands for Infrared Red, which is a type of electromagnetic wave that is not visible to the human eye. We experience this type of radiation as thermal energy, or heat, and although we are unable to see it, an IR radar can surely detect it.

What is a stepper motor?

A stepper motor is like a regular DC motor, but it turns in a certain amount of steps per revolution. In this case we will be using a 200 steps/rev motor, which means that the motor will make a complete turn in 200 steps. So with each step it will turn 1.8o (number of steps/360o). This allows us to have very precise rotations and/or slow rotations with high torque.  

Materials

Wiring

Get a piece of wood and drill a hole of the size of the the motor’s stick.

 

Wood Block with Drilled Hole

Wood Block with Drilled Hole 

Insert the motor in the piece of wood and paste it to a base upside down.

 

Placement of Motor in Hole

Placement of Motor in Hole

 

Wood Block Base

Wood Block Base

 

Paste another base to the bottom of the motor.

 

Motor Base .

Motor Base

 

Connect the Dual Motor Shield to the Arduino and paste it on top of of the second base.

 

Dual Motor Shield (Left). Arduino UNO (Right).

Dual Motor Shield (Left). Arduino UNO (Right)

 

 

Placement of Dual Motor Shield Mounted on Arduino UNO

Placement of Dual Motor Shield Mounted on Arduino UNO

 

 Placement of Arduino UNO on Motor Base

Placement of Arduino UNO on Motor Base

 

Trim as necessary and connect the wires from the motor to the shield.

 

Motor Wires Connection: Blue (3), Red (4), Black (2), Green (1)

Motor Wires Connection: Blue (3), Red (4), Black (2), Green (1)

 

In the protoboard place the Arduino Mini Pro.

 

Arduino Mini Pro

 

Arduino Mini Pro

 

 

Connect the positive side of the buzzer to pin 3 and the negative side to ground.

 

Buzzer Connection

 

Buzzer Connection

 

 

 

Connect the cathode side of an LED to ground, and the anode side to a 330 ohm resistor.

 

 LED Connection

LED Connection

 

Connect the other end of the resistor to pin 5.

 

Resistor Connection

 

Resistor Connection

 

 

 

Connect the IR sensor to the JST connector.

 

 IR Sensor (Left). JST Connector (Right)

IR Sensor (Left). JST Connector (Right)

 

 

 

JST and IR Sensor Connection

JST and IR Sensor Connection

 

 

Note that we adapted some wires to the JST connector so that we could connect them to the protoboard.

 

Connect the sensor to the Arduino Mini Pro.

 

IR Sensor and Arduino Mini Pro Connection

 

IR Sensor and Arduino Mini Pro Connection

 

 

IR Sensor

Arduino Mini Pro

Red Wire

VCC

Black Wire

GND

Yellow Wire

Pin A0

 

Paste the protoboard on the back of the Dual Motor shield.

 

 

Placement of Breadboard on Dual Motor Shield

 

Placement of Breadboard on Dual Motor Shield

 

 Connect the Vin pin from the Arduino UNO to the RAW pin in the Arduino Mini Pro.

 

Power Connection

 Power Connection 

 

Connect the GND from the Arduino UNO to the GND in the Arduino Mini Pro.

 

Ground Connection

Ground Connection

 

Place the battery in the battery holder. Attach a switch and connect it to the Arduino Motor Shield.

 

9V Battery with Switch

 

9V Battery with Switch

 

Placement of Battery on Dual Motor Shield

Placement of Battery on Dual Motor Shield 

Place the Sensor on top of the battery.

 

Figure 21. Complete Wiring Setup

 

Complete Wiring Setup

 

Code 

 

This is the code for the Arduino Motor Shield. Download this to the Arduino Uno board.

#include   //Library to control stepper 

const int stepsPerRevolution = 200;  //Number of steps per revolution of our motor 
Stepper myStepper(stepsPerRevolution, 7,8);  //Initialize stepper library on pins 7 and 8 

int PWMA = 3;  //PWM for Output A 
int PWMB = 5;   //PWM for Output B 
int motor_speed = 10;  // Speed of motor (RPM). Change this to make the motor go faster or slower

void setup() {
  
  myStepper.setSpeed(motor_speed);  //Set the speed of motor to 10 RPM
  analogWrite(PWMA, 255);  //Set Speed for Output A (0-255) was added to turn on Channel A
  analogWrite(PWMB, 255);  //Set Speed for Output B (0-255) was added to turn on Channel B
  
  Serial.begin(9600);  //Set baud rate
}

void loop()
{
  myStepper.step(stepsPerRevolution);  //Turn motor
}

This is the code for the IR sensor. Download this code to the Arduino Pro Mini.

int sensorPin = A0;  //IR sensor pin     
int ledPin = 5;  //LED pin   
int buzzer = 3;  //Buzzer pin 
int sensorValue = 0;  //Initialize sensor reading value to zero
int range = 400;  //Change this to increase or decrease the IR sensor range 

void setup() {
  Serial.begin(9600);  //Set baud rate 
  pinMode(ledPin, OUTPUT);  //Set LED as output 
  digitalWrite(ledPin, LOW);  //Turn LED off
  pinMode(buzzer, OUTPUT);  //Set buzzer as output 
  digitalWrite(buzzer, LOW);  //Turn buzzer off 
}

void loop() {
  sensorValue = analogRead(sensorPin);  //Read sensro value 
  Serial.println(sensorValue);  //Print sensor value on Serial Monitor 
  if(sensorValue > range)  //Chack if sensor is within range 
{  
  digitalWrite(buzzer, HIGH);  //Turn buzzer on 
  digitalWrite(ledPin,HIGH);   //Turn LED on 
}
 else  //Sensor is not within range 
 {
  digitalWrite(ledPin, LOW);  //Turn LED off 
  digitalWrite(buzzer, LOW);  //turn buzzer off       
 }
}

The reason we use two Arduinos is because implementing the motor and sensor with the same microcontroller makes the analog reading from the sensor too slow. This is due to the command myStepper.step(stepsPerRevolution) in the loop. This is a blocking function which means that it will wait until the motor has finished moving to keep executing the rest of the code. So, while the motor is rotating we are missing some analog inputs. This means that we can miss the reading when the sensor detects the presence of an object. This is why one Arduino is in charge of the motor while the other is in charge of the sensor.

Code Explanation

Motor

#include //Library to control stepper 

This is the library required to control the stepper motor.

const int stepsPerRevolution = 200;  //Number of steps per revolution of our motor 
Stepper myStepper(stepsPerRevolution, 7,8);  //Initialize stepper library on pins 7 and 8

Here we specify the number of steps per revolution of our motor as indicated in the motor’s specs. We also initialize the Stepper library on the pins used to give direction to the motor.

   

 int PWMA = 3; //PWM for Output A 
int PWMB = 5;   //PWM for Output B 
int motor_speed = 10;  // Speed of motor (RPM). Change this to make the motor go faster or slower

Here we declare the pins used to pulse width modulate channels A and B. We also specify how fast we want our motor to turn in revolutions per minute.

 

void setup() {
  myStepper.setSpeed(motor_speed);  //Set the speed of motor to 10 RPM
  analogWrite(PWMA, 255);  //Set Speed for Output A (0-255) was added to turn on Channel A
  analogWrite(PWMB, 255);  //Set Speed for Output B (0-255) was added to turn on Channel B
  
  Serial.begin(9600);  //Set baud rate
}

Here we use the RPM value defined before and set the motor to turn at this speed. Then we set the pulse width modulation for channels A and B. The lower the value, the slower the speed.

 

void loop(){

  myStepper.step(stepsPerRevolution);  //Turn motor
}

Here we make the motor turn clockwise at the speed previously set.


IR Sensor

     

int sensorPin = A0; //IR sensor pin  int ledPin = 5;  //LED pin   
int buzzer = 3;  //Buzzer pin 
int sensorValue = 0;  //Initialize sensor reading value to zero
int range = 400;  //Change this to increase or decrease the IR sensor range 

Here we declare the pins used for the sensor, LED, and buzzer. We also initialize the value of the sensor to zero and set its range to 400 which is roughly 15cm.

 

void setup() {  Serial.begin(9600);  //Set baud rate 
  pinMode(ledPin, OUTPUT);  //Set LED as output 
  digitalWrite(ledPin, LOW);  //Turn LED off
  pinMode(buzzer, OUTPUT);  //Set buzzer as output 
  digitalWrite(buzzer, LOW);  //Turn buzzer off 
}

In the setup section we set the baud rate, we declare LED and buzzer as outputs, and then turn them off.

 

void loop() {  sensorValue = analogRead(sensorPin); 
  Serial.println(sensorValue);
  if(sensorValue > range)
{  
  digitalWrite(buzzer, HIGH);
  digitalWrite(ledPin,HIGH); 
}
 else
 {
  digitalWrite(ledPin, LOW);
  digitalWrite(buzzer, LOW);                    
 }
}

In the loop we constantly read the analog values; they are read by the sensor and print them on the serial monitor. Then we check if the values are greater than the range defined. If the case is true, we turn the LED and buzzer on. If the values are less than the range...nothing happens.  

Results