1. HC-SR501 Human Body infrared sensor module test
// Infrared sensing
// Signal to port 7
Int Ledpin = 7 ;
VoidSetup ()
{
Pinmode (ledpin, input );
Serial. Begin (9600);//Open the serial port and set the baud rate to 9600 bps.
}
VoidLoop ()
{
Int In= Digitalread (ledpin );
Serial. println (In);//When someone is there, the output is high. 1 nobody is 0.
Delay (2000);
}
I bought a module in this way: if it is placed on a car, it can sense whether there are people around it. If no one is running, it will only get out when someone is there, haha ..
2. Test the Ultrasonic Ranging Module
// Ultrasonic Ranging
// ECHO is connected to arduino.5; Trig is connected to arduino.4
// VCC +, end-Gnd can be tested without external power supply
Const Int Trigpin = 4 ;
Const Int Echopin = 5 ;
VoidSetup (){
//Initialize serial communication:
Serial. Begin (9600);
}
VoidLoop ()
{
//Establish variables for duration of the Ping,
//And the distance result in centimeters:
LongDuration, cm;
// the HC-SR04 is triggered by a high pulse of 2 or more microseconds.
// give a short low pulse beforehand to ensure a clean high pulse:
pinmode (trigpin, output);
digitalwrite (trigpin, low);
delaymicroseconds ( 2 );
digitalwrite (trigpin, high);
delaymicroseconds ( 5 );
digitalwrite (trigpin, low);
// the same pin is used to read the signal from the HC-SR04: A high
/// pulse whose duration is the time (in microseconds) from the sending
/// Of the ping to the alert of its echo off of an object.
pinmode (echopin, input);
duration = pulsein (echopin, high);
//Convert the time into a distance
Cm = microsecondstocentimeters (duration );
Serial. Print (CM );
Serial. Print ("Cm");
Serial. println ();
Delay (1000);
}
long microsecondstocentimeters ( long microseconds)
{< br> /// the speed of sound is 340 m/s or 29 microseconds per centimeter.
// the ping travels out and back, so to find the distance of the
/// object we take half of the distance traveled.
return microseconds/ 29 / 2 ;
}
The first law of the Three Laws of robots: robots should not harm humans, or stand by and watch them hurt. The second law must obey human commands unless it violates the first law; the third law does not violate the first and second rules. Robots must protect themselves.
With the ultrasonic module, the distance between the vehicle and the obstacle can be detected and the distance can be determined. Then we can protect ourselves and comply with the third law.
3. DC motor control module test
// DC motor control
// External Power supply required 5 V-12 V
// Connect VCC to Arduino + 5 And Gnd to Arduino Gnd
// 1ina 1inb is connected to Arduino PWM 6, 7
Int Leftmotorpin1 = 7 ; // Define your motor pins
Int Leftmotorpin2 = 6 ;
Void Setup ()
{
For ( Int Pinindex = 0 ; Pinindex < 14 ; Pinindex ++ ){
Pinmode (pinindex, output ); // Set pins 0 to 13 as outputs
}
Serial. Begin ( 9600 ); // Open the serial port and set the baud rate to 9600 bps.
}
Void Loop ()
{
Serial. println ( " Run " );
Digitalwrite (leftmotorpin1, high );
Digitalwrite (leftmotorpin2, low );
Delay ( 5000 );
Serial. println ( " Back " );
Digitalwrite (leftmotorpin1, low );
Digitalwrite (leftmotorpin2, high );
Delay (5000 );
Serial. println ( " Stop " );
Digitalwrite (leftmotorpin1, low );
Digitalwrite (leftmotorpin2, low );
Delay ( 3000 );
}
With the motor drive, the vehicle can be controlled to move forward and backward. You can also turn left, turn right, turn around, and other functions by using the two-wheel speed difference. At first, I used the driver module and found that the module had serious signal interference with the ultrasonic module. Finally, I switched to the motor drive with photocoupler to solve this problem.
Motor Drive with optical coupling:
4. Servo Control Test
//Servo Control Test
// External Power Supply + 5 V-+ 12 V required
// Steering Gear connected to 5 + Arduino. Gnd models connected to Arduino. PWM 9
# Include <servo. h>
Servo myservo; // Define the variable name of the steering gear
Void Setup ()
{
Myservo. Attach ( 9 ); // Define the steering gear interface, 9 or 10
Serial. Begin ( 9600 ); // Open the serial port and set the baud rate to 9600 bps.
}
Void Loop ()
{
For ( Int I = 0 ; I <= 18 ; I ++)
{
Myservo. Write (I * 10 ); // Set the Steering Wheel Rotation Angle
Serial. println (I * 10 );
Delay ( 1000 );
}
}
Note that the current of the steering gear is relatively large and external power supply is required. The 5 V power supply on the android board cannot be used directly.