KS0507 Keyestudio Smart RV Kit: Difference between revisions

From Keyestudio Wiki
Jump to navigation Jump to search
 
(229 intermediate revisions by the same user not shown)
Line 215: Line 215:


<br>[[Image:0507--34.png|400px|frameless]]<br>
<br>[[Image:0507--34.png|400px|frameless]]<br>


== Projects ==
== Projects ==
Line 332: Line 331:
If output five hundred 1, that is 5V; if all of which is 1, that is 0V. If output 010101010101 in this way then the output port is 2.5V, which is like showing movie. The movie we watch are not completely continuous. It actually outputs 25 pictures per second. In this case, the human can’t tell it, neither does PWM. If want different voltage, need to control the ratio of 0 and 1. The more 0,1 signals output per unit time, the more accurately control.<br>
If output five hundred 1, that is 5V; if all of which is 1, that is 0V. If output 010101010101 in this way then the output port is 2.5V, which is like showing movie. The movie we watch are not completely continuous. It actually outputs 25 pictures per second. In this case, the human can’t tell it, neither does PWM. If want different voltage, need to control the ratio of 0 and 1. The more 0,1 signals output per unit time, the more accurately control.<br>
(2)Component:<br>
(2)Component:<br>
<br>[[File:0507--9.png|500px|frameless|thumb]]<br>
<br>[[File:QQ图片20210901132133.png|500px|frameless|thumb]]<br>
(3)Connection Diagram:<br>
(3)Connection Diagram:<br>
We keep connection diagram unchanged.<br>
We keep connection diagram unchanged.<br>
<br>[[File:0507--10.png|500px|frameless|thumb]]<br>
<br>[[File:QQ图片20210901132135.png|500px|frameless|thumb]]<br>
(4)Test Code:<br>
(4)Test Code:<br>
<pre>
<pre>
Line 433: Line 432:


(1)Description:
(1)Description:
In this project, we control LED brightness by button module. The low level(0) is output when the button is pressed; however, high level(1) is output when button is released.
In this project, we control LED brightness by button module. The low level(0) is output when the button is pressed; however, high level(1) is output when button is released.


Line 446: Line 446:
(3)Component:
(3)Component:


<br>[[File:0507--.png|500px|frameless|thumb]]<br>
<br>[[File:0507--补1.png|500px|frameless|thumb]]<br>


(4)Connection Diagram:<br>
(4)Connection Diagram:<br>
Line 476: Line 476:


(6)Test Result:<br>
(6)Test Result:<br>
Upload code, open serial monitor to set baud rate to 9600. The number “1”shows up then“0”appears on the monitor.<br>
Upload code, open serial monitor to set baud rate to 9600. The number “1” shows up then “0” appears on the monitor.<br>


<br>[[File:0507--14.png|500px|frameless|thumb]]<br>
<br>[[File:0507--14.png|500px|frameless|thumb]]<br>
Line 540: Line 540:
<span style=color:red>Note: Adjust the sensitivity by rotating the potentiometer before the test. The sensitivity is best when LED is at on and off state.</span><br>
<span style=color:red>Note: Adjust the sensitivity by rotating the potentiometer before the test. The sensitivity is best when LED is at on and off state.</span><br>
(3) Component:<br>
(3) Component:<br>
<br>[[File:0507--.png|500px|frameless|thumb]]<br>
<br>[[File:0507--补2.png|500px|frameless|thumb]]<br>
(4)Connection Diagram:<br>
(4)Connection Diagram:<br>
<br>[[File:0507--17.png|500px|frameless|thumb]]<br>
<br>[[File:0507--17.png|500px|frameless|thumb]]<br>
Line 630: Line 630:
</pre>
</pre>
Upload the code to development board, we observe LED get brighter when covering the line tracking sensor by hand.
Upload the code to development board, we observe LED get brighter when covering the line tracking sensor by hand.
=== Project 5: PIR Motion Sensor  ===
=== Project 5: PIR Motion Sensor  ===
(1)Description:<br>
(1)Description:<br>
Line 651: Line 652:
5. Done powering up and uploading the code, you need to wait 5-10 seconds then start testing, otherwise it is not sensitive.</span><br>
5. Done powering up and uploading the code, you need to wait 5-10 seconds then start testing, otherwise it is not sensitive.</span><br>
(3)Component:<br>
(3)Component:<br>
<br>[[File:0507--.png|500px|frameless|thumb]]<br>
<br>[[File:0507--补3.png|500px|frameless|thumb]]<br>
(4)Connection Diagram<br>
(4)Connection Diagram<br>
<span style=color:red>Note: on expansion board, the pin G, V and S of PIR motion sensor are connected to G, V and 47; pin G, V and S of LED module are connected to G, V and 13.</span><br>
<span style=color:red>Note: on expansion board, the pin G, V and S of PIR motion sensor are connected to G, V and 47; pin G, V and S of LED module are connected to G, V and 13.</span><br>
Line 732: Line 733:
* Port:digital port
* Port:digital port
(3)Component:<br>
(3)Component:<br>
<br>[[File:0507--.png|500px|frameless|thumb]]<br>
<br>[[File:0507--补4.png|500px|frameless|thumb]]<br>




Line 780: Line 781:
Download code, open serial monitor and set baud rate to 9600. The serial monitor will show the corresponding key value when the key of membrane keypad is pressed, as shown below;<br>
Download code, open serial monitor and set baud rate to 9600. The serial monitor will show the corresponding key value when the key of membrane keypad is pressed, as shown below;<br>
<br>[[File:0507--B.png|500px|frameless|thumb]]<br>
<br>[[File:0507--B.png|500px|frameless|thumb]]<br>
=== Project 7: Power Amplifier Module ===   
=== Project 7: Power Amplifier Module ===   
(1)Description:<br>
(1)Description:<br>
Line 791: Line 793:
* Working voltage: DC 3.3-5V
* Working voltage: DC 3.3-5V
(3) Component:
(3) Component:
<br>[[File:0507--.png|500px|frameless|thumb]]<br>
<br>[[File:0507--补5.png|500px|frameless|thumb]]<br>
(4) Connection Diagram:<br>
(4) Connection Diagram:<br>
<span style=color:red>Note: the pin G, V and S of passive buzzer are connected to pin G, V and D49 of expansion board</span><br>
<span style=color:red>Note: the pin G, V and S of passive buzzer are connected to pin G, V and D49 of expansion board</span><br>
Line 924: Line 926:


(3)Component:
(3)Component:
<br>[[File:0507--.png|500px|frameless|thumb]]<br>
 
<br>[[File:0507--补7.png|500px|frameless|thumb]]<br>


(4)Connection Diagram:<br>
(4)Connection Diagram:<br>
<br>[[File:0507--E.png|500px|frameless|thumb]]<br>
<br>[[File:0507--E.png|500px|frameless|thumb]]<br>


Line 1,001: Line 1,005:
}
}
</pre>
</pre>
=== Project 9: Adjustable Rotary Potentiomete ===
=== Project 9: Adjustable Rotary Potentiomete ===
(1)Description:
(1)Description:
Line 1,010: Line 1,015:
* Interface: analog interface
* Interface: analog interface
(3)Component:
(3)Component:
<br>[[File:0507--.png|500px|frameless|thumb]]<br>
<br>[[File:0507--补8.png|500px|frameless|thumb]]<br>


(4)Connection Diagram:<br>
(4)Connection Diagram:<br>
Line 1,040: Line 1,045:
3. Serial monitor shows the value of variable value, and set baud rate to 9600.<br>
3. Serial monitor shows the value of variable value, and set baud rate to 9600.<br>
analogRead(pin)-read the analog value of pin, mega2560 control board had 16 analog ports (A0-A15), in the range of 0~1023,that is, the accuracy is 5V/1024=0.0049V.<br>
analogRead(pin)-read the analog value of pin, mega2560 control board had 16 analog ports (A0-A15), in the range of 0~1023,that is, the accuracy is 5V/1024=0.0049V.<br>
map(value, fromLow, fromHigh, toLow, toHigh)-value is mapped value,fromLow- current value range lower limit,fromHigh- current value range upper limit,toLow- target value range lower limit,toHigh-target value range upper limit.<br>
map(value, fromLow, fromHigh, toLow, toHigh)-value is mapped value,fromLow- current value range lower limit,fromHigh- current value range upper limit,toLow- target value range lower limit,toHigh-target value range upper limit.<br>
(7) Test Result:<br>
(7) Test Result:<br>
Upload code, power on with USB cable, open serial monitor and set baud rate to 9600. The serial monitor shows the corresponding analog value.<br>  
Upload code, power on with USB cable, open serial monitor and set baud rate to 9600. The serial monitor shows the corresponding analog value.<br>  
In the project, rotate the potentiometer clockwise, and the analog value increases; yet, the analog value(0-1023) decreases if rotating anticlockwise, as shown below,<br>
In the project, rotate the potentiometer clockwise, and the analog value increases; yet, the analog value(0-1023) decreases if rotating anticlockwise, as shown below,<br>
<br>[[File:0507--J.png|500px|frameless|thumb]]<br>
(8)Project Extension:control LED brightness by potentiometer<br>
<br>[[File:0507--K.png|500px|frameless|thumb]]<br>
<pre>
/*
  keyestudio smart motorhome
  lesson 9.2
  Adjustable potentiometer
  http://www.keyestudio.com
*/
#define RES A3  //set potentiometer pin to A3
#define LED 13  //define the pin of LED as D13
volatile int value = 0;
void setup() {
  pinMode(LED, OUTPUT);// initialize digital pin LED as an output.
}
void loop () {
  value = analogRead(RES);  //read the value of potentiometer
  int light = map(value, 0, 1023, 0, 255); //map the analog value 0~1023 of potentiometer to 0~255
  analogWrite(LED, light);  //set LED brightness to light
  delay(100);              //delay in 100MS
}
</pre>
=== Project10: Ambient Light Sensor ===
(1)Description:<br>
<br>[[File:0507--L.png|500px|frameless|thumb]]<br>
Ambient light sensor adopts TEMT6000 element which is a high sensitivity and visible photo sensitive (NPN type) triode. It can catch tiny light ray, amplify for 100 times and be recognized by MCU. What’s more, the reaction to light is similar to human eyes, which can determine light intensity and help create interactive appliances.
<br>
(2) Specification:<br>
* Working voltage: 3.3V-5V (DC)
* Interface: 3PIN interface
* Output signal: analog signal
* Weight: 2.3g
<br>[[File:0507--M.png|500px|frameless|thumb]]<br>
(4) Connection Diagram:<br>
<span style=color:red>Note: pin G,V and S of ambient light sensor are separately interfaced with G, V and A4; pin G, V and S of LED module are connected to G, V and 13.</span><br>
<br>[[File:0507--N.png|500px|frameless|thumb]]<br>
(5) Test Code:
<pre>
//****************************************************************************
/*
  keyestudio smart motorhome
  lesson 10.1
  photovaristor
  http://www.keyestudio.com
*/
#define photos  A4  //set photoresistance pin to A4
#define LED 13  //define the pin of LED as D13
volatile int value = 0;
void setup() {
  Serial.begin(9600);
  pinMode(LED, OUTPUT);// initialize digital pin LED as an output.
}
void loop () {
  value = analogRead(photos);  //read the value of photoresistor
  Serial.println(value);
  if (value < 300) {  //when the analog vlaue is les than 300
    digitalWrite(LED, HIGH); //turn on LED
  }
  else {  //when the analog value is higher than 300
    digitalWrite(LED, LOW); //turn off LED
  }
  delay(100);              //delay in 100MS
}
//***************************************************************************
</pre>
(6) Test Result:<br>
Wire up, upload code and set baud rate to 9600. We see the value detected by ambient light sensor.<br>
The value reduces when covering ambient sensor, however, LED will be on when the value is less than 300; on the contrary, LED will be off.<br>
<br>[[File:0507--O.png|500px|frameless|thumb]]<br>
=== Project 11:Steam Sensor ===
<br>[[File:0507--P.png|500px|frameless|thumb]]<br>
(1)Description:<br>
This is a commonly used steam sensor. Its principle is to detect the amount of water by bare printed parallel lines on the circuit board. The more the water is, the more wires will be connected. As the conductive contact area increases, the output voltage will gradually rise. It can detect water vapor in the air as well. The steam sensor can be used as a rain water detector and level switch. When the humidity on the sensor surface surges, the output voltage will increase.<br>
The sensor is compatible with various microcontroller control boards, such as Arduino series microcontrollers. When using it, we provide the guide to operate steam sensor and Arduino control board. Connect the signal end of the sensor to the analog port of the microcontroller, sense the change of the analog value, and display the corresponding analog value on the serial monitor.<br>
<span style=color:red>Note: the connect part is not waterproof, don’t immerse it in the water please.</span><br>
(7)Specification:
Working voltage: DC 3.3-5V
Working current: <20mA
Operating temperature range: -10℃~+70℃;
Control signal: analog signal output
Interface: 2.54mm 3pin pin interface
Size: 35*20*8mm
Weight: 2.2g
3. Pin Description:<br>
S:signal pin output<br>
V(+):Power(VCC)<br>
G(-):Ground(GND)<br>
(3) Component:<br>
<br>[[File:0507--Q.png|500px|frameless|thumb]]<br>
(4) Connection Diagram:<br>
<span style=color:red>Note: on expansion board, pin G, V and S of steam sensor are connected to G, V and A2.<br></span><br>
<br>[[File:0507--R.png|500px|frameless|thumb]]<br>
(6)Test Code:<br>
<pre>
//*************************************************************************
/*
  keyestudio smart motorhome
  lesson 11.1
  water sensor
  http://www.keyestudio.com
*/
#define waterpin A5 //water sensor pin to A5
volatile int water = 0;
void setup() {
  Serial.begin(9600);
}
void loop () {
  water = analogRead(waterpin);  //read the analog value of water sensor
  Serial.println(water);
  delay(100);
}
//******************************************************************************
</pre>
(7)Test Result:<br>
Upload code, power on according to connection diagram, open serial monitor and set baud rate to 9600. The value on monitor increases when the water is on steam sensor; whereas, the value decreases when the water is wiped off.<br>
<br>[[File:0507--S.png|500px|frameless|thumb]]<br>
(8)Project Extension:
Power amplifier module will alarm when the water content exceeds 300<br>
<br>[[File:0507--T.png|500px|frameless|thumb]]<br>
<pre>
/*
  keyestudio smart motorhome
  lesson 11.2
  water sensor
  http://www.keyestudio.com
*/
#define waterpin A5 //define the pin of water sensor as A5
#define buzzer  49 //define the pin of buzzer as D49
volatile int water = 0;
void setup() {
  Serial.begin(9600);
  pinMode(buzzer, OUTPUT);//set buzzer to OUTPUT
}
void loop () {
  water = analogRead(waterpin);  //read the analog value of water sensor
  Serial.println(water);
  if (water > 300) {  //when the analog value is more than 300
    tone(buzzer,2000); //buzzer emits the sound with 2000Hz
  }
  else {  //when the analog value is less than 300
    noTone(buzzer); //turn off buzzer
  }
 
}
</pre>
=== Project 12 Analog Gas(MQ-2)Sensor ===
(1)Description:<br>
This gas sensor is used for household gas leak alarms, industrial combustible gas alarms and portable gas detection instruments. And it is suitable for the detection of liquefied gas, benzene, alkane, alcohol, hydrogen, etc., and widely used in various fire alarm systems. The MQ-2 smoke sensor can be accurately a multi-gas detector, and has the advantages of high sensitivity, fast response, good stability, long life, and simple drive circuit.
It can detect the concentration of flammable gas and smoke in the range of 300~10000ppm.Meanwhile, it has high sensitivity to natural gas, liquefied petroleum gas and other smoke, especially to alkanes smoke.
It must be heated for a period of time before using the smoke sensor, otherwise the output resistance and voltage are not accurate. However, the heating voltage should not be too high, otherwise it will cause my internal signal line to blow.<br>
It is belongs to the tin dioxide semiconductor gas-sensitive material, and belongs to the surface ion type N-type semiconductor. At a certain temperature, tin dioxide adsorbs oxygen in the air and forms negative ion adsorption of oxygen, reducing the electron density in the semiconductor, thereby increasing its resistance value. When in contact with flammable gas in the air and smog, if the potential barrier at the grain boundary is adjusted by the smog, it will cause the surface conductivity to change. With this, information about the presence of smoke or flammable gas can be obtained. The greater the concentration of smoke or flammable gas in the air, the greater the conductivity, and the lower the output resistance, the larger the analog signal output. The sensor comes with a positioning hole, which is convenient for you to fix the sensor to other devices. In addition, the sensitivity can be adjusted by rotating the potentiometer.<br>
(2) Specification:<br>
* Working voltage: 3.3-5V (DC)
* Interface: 4 pins (GND, VCC, D0, A0)
* Output signal: digital signal and analog signal
* Weight: 7.5g
(3) Component:<br>
<br>[[File:0507--U.png|500px|frameless|thumb]]<br>
(4) Connection Diagram:<br>
Note: on expansion board, the pin<span style=color:red> GND, VCC, D0 and A0 </span>of analog gas<span style=color:red> (MQ-2)sensor</span> are interfaced with G,V, A1 and A0.<br>
<br>[[File:0507--V.png|500px|frameless|thumb]]<br>
(5) Test Code:
<pre>
//********************************************************************************
/*
  keyestudio smart motorhome
  lesson 12.1
  MQ2
  http://www.keyestudio.com
*/
#define MQ2_A  A0  //define the pin mq2 as A0
#define MQ2_D  A1  //define the pin mq2 as A1
volatile int analogVal,digitalVal;
void setup() {
  Serial.begin(9600);
}
void loop () {
  digitalVal = digitalRead(MQ2_D);  //read the digital value
  analogVal = analogRead(MQ2_A);  //read the analog value
  Serial.print("digitalVal:");
  Serial.print(digitalVal);
  Serial.print("  analogVal:");
  Serial.println(analogVal);
  delay(100);
}
//******************************************************************************
</pre>
(6) Test Result:<br>
Wire up, upload code and open serial monitor to set baud rate to 9600.<br>
The analog value increases and digital level varies from 1 to 0 when the combustible gas is detected.<br>
<br>[[File:0507--W.png|500px|frameless|thumb]]<br>
(7) Project Extension:<br>
Power amplifier module will alarm when the detected combustible gas is leaked.<br>
<br>[[File:0507--H.png|400px|frameless|thumb]]<br>
<pre>
/*
  keyestudio smart motorhome
  lesson 12.2
  MQ2
  http://www.keyestudio.com
*/
#define MQ2_A  A0  // define MQ2_A as A0
#define MQ2_D  A1  //define MQ2_A as A1
#define buzzer  49 //buzzer pin to D49
volatile int analogVal, digitalVal;
void setup() {
  Serial.begin(9600);
}
void loop () {
  analogVal = analogRead(MQ2_A);  //read the analog value
  Serial.println(analogVal);      //print the value of sensor
  if (analogVal > 150) {          //when analog value exceeds 150, the buzzer will alarm
    tone(buzzer, 1000);
  }
  else {
    noTone(buzzer); //turn off buzzer
  }
  delay(100);
}
</pre>
=== Project 13 DHT11 Temperature and Humidity Sensor ===
<br>[[File:0507--I.png|500px|frameless|thumb]]<br>This DHT11 Temperature and Humidity Sensor is a composite sensor which contains a calibrated digital signal output of the temperature and humidity.Its technology ensures high reliability and excellent long-term stability. A high-performance 8-bit microcontroller is connected.This sensor includes a resistive element and a sense of wet NTC temperature measuring devices. It has excellent quality, fast response, anti-interference ability and high cost performance advantages.Each DHT11 sensor features extremely accurate calibration data of humidity calibration chamber. The calibration coefficients stored in the OTP program memory, internal sensors detect signals in the process, and we should call these calibration coefficients.The single-wire serial interface system is integrated to make it quick and easy. Qualities of small size, low power, and 20-meter signal transmission distance make it a wide applied application or even the most demanding one. <br>
(2) Specification:<br>
* Supply Voltage: +5 V
* Temperature range: 0-50 ℃ error of ± 2 ℃
* Humidity: 20-90% RH ± 5% RH error
* Interface: Digital
(3) Component:<br>
<br>[[File:0507--X.png|500px|frameless|thumb]]<br>
(8)Connection Diagram:<br>
<br>[[File:0507--图片1.png|400px|frameless|thumb]]<br>
<span style=color:red>In the project, we need to import the library of DHT11.</span><br>
(9)Test Code:<br>
<pre>
/*
  keyestudio smart motorhome
  lesson 13.1
  DHT11
  http://www.keyestudio.com
*/
#include <dht11.h>  //include the library code:
dht11 DHT;
#define DHT11_PIN 47 //define DHT11 as 48
void setup() {
  Serial.begin(9600);
}
void loop () {
  int chk;
  chk = DHT.read(DHT11_PIN);    // READ DATA
  switch (chk) {
    case DHTLIB_OK:
      break;
    case DHTLIB_ERROR_CHECKSUM:
      break;
    case DHTLIB_ERROR_TIMEOUT:
      break;
    default:
      break;
  }
  // DISPLAT DATA
  Serial.print("humidity:");
  Serial.print(DHT.humidity);
  Serial.print("  temperature:");
  Serial.println(DHT.temperature);
  delay(100);
}
</pre>
(10)Test Result:<br>
Upload code, power on with USB cable,open serial monitor and the temperature and humidity data is shown below:<br>
<br>[[File:0507--Z.png|500px|frameless|thumb]]<br>
=== Project 14 Servo Control === 
<br>[[File:0507--图片2.png|300px|frameless|thumb]]<br>
(1)Description <br>
Servo motor is a position control rotary actuator. It mainly consists of housing, circuit board, core-less motor, gear and position sensor. Its working principle is that the servo receives the signal sent by MCU or receiver and produces a reference signal with a period of 20ms and width of 1.5ms, then compares the acquired DC bias voltage to the voltage of the potentiometer and obtain the voltage difference output.<br>
When the motor speed is constant, the potentiometer is driven to rotate through the cascade reduction gear, which leads that the voltage difference is 0, and the motor stops rotating. Generally, the angle range of servo rotation is 0° --180 °<br>
The rotation angle of servo motor is controlled by regulating the duty cycle of PWM (Pulse-Width Modulation) signal. The standard cycle of PWM signal is 20ms (50Hz). Theoretically, the width is distributed between 1ms-2ms, but in fact, it's between 0.5ms-2.5ms. The width corresponds the rotation angle from 0° to 180°. But note that for different brand motor, the same signal may have different rotation angle. <br>
<br>[[File:0507--图片3.png|300px|frameless|thumb]]<br>
In general, servo has three line in brown, red and orange. Brown wire is grounded, red one is positive pole line and orange one is signal line.<br>
<br>[[File:0507--图片4.png|500px|frameless|thumb]]<br>
The corresponding servo angles are shown below:
<br>[[File:0507--图片5.png|500px|frameless|thumb]]<br>
(2)Specification<br>
Working voltage: <br>
* DC 4.8V ~ 6V
* Operating angle range: about 180 ° (at 500 → 2500 μsec)
* Pulse width range: 500 → 2500 μsec
* No-load speed: 0.12 ± 0.01 sec / 60 (DC 4.8V) 0.1 ± 0.01 sec / 60 (DC 6V)
* No-load current: 200 ± 20mA (DC 4.8V) 220 ± 20mA (DC 6V)
* Stopping torque: 1.3 ± 0.01kg · cm (DC 4.8V) 1.5 ± 0.1kg · cm (DC 6V)
* Stop current: ≦ 850mA (DC 4.8V) ≦ 1000mA (DC 6V)
* Standby current: 3 ± 1mA (DC 4.8V) 4 ± 1mA (DC 6V)
(3) Component:
<br>[[File:0507--图片6.png|500px|frameless|thumb]]<br>
(4) Connection Diagram:<br>
<span style=color:red>Note: The servo is connected to G (GND), V (VCC),8. The brown wire of the servo is connected to Gnd (G), the red wire is linked with 5v (V), and the orange wire is connected to digital pin 8.<br>
Remember to connect external power due to the high current of driving motor.</span><br>
<br>[[File:0507--图片7.png|400px|frameless|thumb]]<br>
(6)Test Code1:
<pre>
/*
  keyestudio smart motorhome
  lesson 14.1
  servo
  http://www.keyestudio.com
*/
int servoPin = 8; //pin of servo
void setup() {
  pinMode(servoPin, OUTPUT);//set the pin of servo to OUTPUT
}
void loop() {
  servopulse(servoPin, 0);//rotate to 0°
  delay(1000);//delay 1s
  servopulse(servoPin, 90);//rotate to 900°
  delay(1000);
  servopulse(servoPin, 180);//rotate to 180°
  delay(1000);
}
void servopulse(int pin, int myangle) { //output function
  int pulsewidth = map(myangle, 0, 180, 500, 2500); //map angle to pulsewidth
  for (int i = 0; i < 10; i++) { //output pulse
    digitalWrite(pin, HIGH);//set level of servo port to HIGH
    delayMicroseconds(pulsewidth);//delay the time of pulsewidth
    digitalWrite(pin, LOW);//set the level of servo port to LOW
    delay(20 - pulsewidth / 1000);
  }
}
//**********************************************************************************
</pre>
Upload code successfully, servo swings forth and back in the range of 0° 90° and 180°.
There is another guide for restraining servo---- servo library file, the following link of official website is for your reference.
https://www.arduino.cc/en/Reference/Servo<br>
(7)Test Code2:<br>
<pre>
/*
  keyestudio smart motorhome
  lesson 14.2
  servo
  http://www.keyestudio.com
*/
#include <Servo.h>  //include the library code:
Servo myservo;
void setup() {
  myservo.attach(8);  //connect servo to digital 8
}
void loop () {
  //rotate 0° to 180°
  for (int i = 0; i < 180; i++) {
    myservo.write(i);
    delay(20);
  }
  delay(1000);  //delay in 1s
  //rotate from 180° to 0°
  for (int i = 180; i > 0; i--) {
    myservo.write(i);
    delay(20);
  }
  delay(1000);  //delay in 1s
}
//********************************************************************************
</pre>
(8)Test Result:<br>
Upload code successfully and power on, servo swings in the range of 0° to 180°. <br>
(9)Code Explanation:<br>
Arduino comes with #include <Servo.h> (servo function and statement)
The following are some common statements of the servo function:<br>
1. attach(interface)——Set the pin of servo.<br>
2. write(angle)——The statement to set rotation angle of servo, the angle range is from 0° to 180°.<br>
3. read()——The statement to read angle of servo, read the command value of“write()”.<br>
4. attached()——Judge if the parameter of servo is sent to its interface <br>
<span style=color:red>Note: The above written format is“servo variable name, specific statement()”, for instance: myservo.attach(8)</span><br>
=== Project 15 Ultrasonic Sensor ===
(1)Description:<br>
The HC-SR04 ultrasonic sensor uses sonar to determine distance to an object like bats do. It offers excellent non-contact range detection with high accuracy and stable readings in an easy-to-use package. It comes complete with ultrasonic transmitter and receiver modules.The HC-SR04 or the ultrasonic sensor is being used in a wide range of electronics projects for creating obstacle detection and distance measuring application as well as various other applications. Here we have brought the simple method to measure the distance with Arduino and ultrasonic sensor and how to use ultrasonic sensor with Arduino.<br>
(2) Specification:<br>
<br>[[File:0507--图片8.png|500px|frameless|thumb]]<br>
(3) Component:<br>
<br>[[File:0507--图片10.png|500px|frameless|thumb]]<br>
(4) The principle of ultrasonic sensor<br>
The ultrasonic module will emit the ultrasonic waves after trigger signal. When the ultrasonic waves encounter the object and are reflected back, the module outputs an echo signal, so it can determine the distance of object from the time difference between trigger signal and echo signal.
The t is the time that emitting signal meets obstacle and returns.<br>
and the propagation speed of sound in the air is about 343m/s, therefore,  distance = speed * time, because the ultrasonic wave emits and comes back, which is 2 times of distance, so it needs to be divided by 2, the distance measured by ultrasonic wave = (speed * time)/2<br>
1.Use method and timing chart of ultrasonic module:<br>
Setting the delay time of Trig pin of SR04 to 10μs at least, which can trigger it to detect distance.<br>
2. After triggering, the module will automatically send eight 40KHz ultrasonic pulses and detect whether there is a signal return. This step will be completed automatically by the module.<br>
3. If the signal returns, the Echo pin will output a high level, and the duration of the high level is the time from the transmission of the ultrasonic wave to the return.<br>
<br>[[File:0507--图片11.png|500px|frameless|thumb]]<br>
Circuit diagram of ultrasonic sensor:<br>
<br>[[File:0507--图片12.png|500px|frameless|thumb]]<br>
(5) Connection Diagram:<br>
<br>[[File:0507--图片13.png|500px|frameless|thumb]]<br>
<span style=color:red>Wiring guide:</span><br>
  <span style=color:red> Ultrasonic sensor            keyestudio V5 Sensor Shield
      VCC          →                5v(V)
    Trig          →                22(S)
    Echo        →                23(S)
    Gnd          →                Gnd(G)<br>
</span><br>
(6) Test Code:
<pre>
/*
  keyestudio smart motorhome
  lesson 15.1
  Ultrasonic sensor
  http://www.keyestudio.com
*/
//Ultrasonic Sensor
#define EchoPin  23  //ECHO to D23
#define TrigPin  22  //TRIG to D22
float dis;
void setup() {
  Serial.begin(9600);
  pinMode(EchoPin, INPUT);  // set pin ECHO to INPUT
  pinMode(TrigPin, OUTPUT);  //set pin TRIG to OUTPUT
}
void loop () {
  digitalWrite(TrigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(TrigPin, HIGH);  //trigger TrigPin at least 10us high level
  delayMicroseconds(10);
  digitalWrite(TrigPin, LOW);
  dis = pulseIn(EchoPin, HIGH) / 2 / 29.1;  //calculate distance
  delay(50);  //delay in 50ms
  Serial.print("distance: ");
  Serial.print(dis);
  Serial.println("cm");
}
//**************************************************************************
</pre>
(7) Test Result:
Upload code to development board, open serial monitor and set baud rate to 9600. The serial monitor shows the distance, and unit is cm. The value reduces when covering the ultrasonic sensor.
<br>[[File:0507--图片14.png|500px|frameless|thumb]]<br>
(8) Code Explanation:<br>
int trigPin- this pin is defined to transmit ultrasonic waves, generally output.<br>
int echoPin - this is defined as the pin of reception, generally input<br>
cm = (duration/2) / 29.1<br>
inches = (duration/2) / 74<br>
We can calculate the distance by using the following formula:<br>
distance = (traveltime/2) x speed of sound<br>
The speed of sound is: 343m/s = 0.0343 cm/uS = 1/29.1 cm/uS<br>
Or in inches: 13503.9in/s = 0.0135in/uS = 1/74in/uS<br>
We need to divide the traveltime by 2 because we have to take into account that the wave was sent, hit the object, and then returned back to the sensor.<br>
(9) Extension Practice:<br>
We’ve known how to detect the distance, next, let’s try to control an LED with detected distance
Value. <br>
Connect an LED module at D13<br>
<br>[[File:0507--图片15.png|500px|frameless|thumb]]<br>
<pre>
/*
  keyestudio smart motorhome
  lesson 15.2
  Ultrasonic sensor
  http://www.keyestudio.com
*/
//Ultrasonic Sensor
#define EchoPin  23  //ECHO to D23
#define TrigPin  22  //TRIG to D22
const int LED = 13;  //led pin to D13
volatile int distance;
void setup() {
  Serial.begin(9600);
  pinMode(EchoPin, INPUT);  //set pin ECHO to INPUT
  pinMode(TrigPin, OUTPUT);  //set pin TRIG to OUTPUT
  pinMode(LED, OUTPUT);
}
void loop () {
  distance = get_distance();  //set distance to variable distance
  Serial.println(distance);
  if (distance < 10) {
    digitalWrite(LED, HIGH);
  }
  else {
    digitalWrite(LED, LOW);
  }
}
int get_distance() {  //detect distance by ultrasonic sensor
  int dis;
  digitalWrite(TrigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(TrigPin, HIGH);  //trigger TRIG at least 10us high level
  delayMicroseconds(10);
  digitalWrite(TrigPin, LOW);
  dis = pulseIn(EchoPin, HIGH) / 58;  //calculate distance
  delay(10);  //delay in 50ms
  return dis;
}
//****************************************************************************                         
</pre>
Upload the above code to development board. Do you find LED on when covering ultrasonic sensor by hand?<br>
=== Project 16: 1602 LCD Display ===
(1)Description:<br>
<br>[[File:0507--图片16.png|500px|frameless|thumb]]<br>
With I2C communication module, this is a display module that can show 2 lines with 16 characters per line.<br>
It shows blue background and white word and connects to I2C interface of MCU, which highly save the MCU resources.<br>
On the back of LCD display, there is a blue potentiometer for adjusting the backlight. The communication address defaults to 0x27.<br>
The original 1602 LCD can start and run with 7 IO ports, but ours is built with ARDUINOIIC/I2C interface, saving 5 IO ports. Alternatively, the module comes with 4 positioning holes with a diameter of 3mm, which is convenient for you to fix on other devices.<br>
Notice that when the screen gets brighter or darker, the characters will become more visible or less visible.<br>
(2) Specification:<br>
* I2C address: 0x27
* Backlight (blue, white)
* Power supply voltage: 5V
* Adjustable contrast
* GND: A pin that connects to ground
* VCC: A pin that connects to a +5V power supply
* SDA: A pin that connects to analog port 20 for IIC communication
* SCL: A pin that connects to analog port 21 for IIC communication
(3) Component:<br>
<br>[[File:0507--图片17.png|500px|frameless|thumb]]<br>
(4) Connection Diagram:<br>
<span style=color:red>Note: the pin GND, VCC, SDA and SCL of 1602LCD module are connected to GND(-), 5V(+), SDA and SCL.</span><br>
<br>[[File:0507--图片18.png|500px|frameless|thumb]]<br>
(5) Test Code:<br>
<pre>
//**************************************************************************
/*
  keyestudio smart motorhome
  lesson 16.1
  I2C 1602
  http://www.keyestudio.com
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h> // includes the LiquidCrystal_I2C Library
LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
void setup() {
  lcd.init();
  // Print a message to the LCD.
  lcd.backlight();  //set backlight
  lcd.setCursor(0,0); /set cursor at the first row and first column
  lcd.print("Hello, World!"); //display "Hello, World!"
  lcd.setCursor(0,1); //at the second row and first column
  lcd.print("Hello, Keyes!"); //show "Hello, Keyes!"
}
void loop () {
 
}
//************************************************************************
</pre>
(6) Test Result:<br>
Upload code, wire up according to connection diagram and power on. 1602 LCD will display“Hello World!”at the first row and show“Hello Keyes!”at the second row.<br> 
<span style=color:red>Note: wire up connection diagram, upload code and power on. You can adjust the potentiometer on the back of 1602LCD display module to display the character strings</span><br>
=== Project 17: 2812 Light Strip ===
                                 
(1)Description:<br>
This module is an intelligent controlled LED light source that integrates the control circuit and light-emitting circuit.
Each LED shape is as same as a 5050LED bead. Each element is a pixel. Each pixel interior includes not only intelligent digital port data latch and signal reshaping amplification drive circuit, but also a precision internal oscillator and a 12V programmable constant current control part, effectively ensuring the highly consistency of the pixel point light color.<br>
The data transfer protocol uses single RZ (return-to-zero) communication mode. After the pixel power-on to reset, the DI port receives data from controller, the first 24bit data is extracted by the first pixel, and then sent to the data latch inside the pixel. After being amplified by the internal shaping processing circuit, the remaining data will be forwarded to the next cascade pixel point through the DO port of the LED.<br>
The signal will be reduced by 24bit for each pixel point transmission. <br>
The pixel point adopts automatic transfer technology so that the number of cascade pixel are only limited by signal transmission speed rather than signal transmission.<br>
(2)Specification:<br>
* LED: WS2812
* Working voltage: DC 5V
* Working current: 40mA*10*2=8000mA (maximum) (each RGB lamp current 40mA)
* Wiring mode: G-GND V-VCC (DC 5V) DI (signal input terminal) DO (signal output terminal, used in cascade connection)
(3)Component:<br>
<br>[[File:0507--图片19.png|500px|frameless|thumb]]<br>
(4) Connection Diagram:<br>
<span style=color:red>Note: the pin GND, VCC and DI of 8x16 LED panel are interfaced with the pin -(GND), +(VCC)and 53 of sensor expansion board.</span><br>
<br>[[File:0507--图片20.png|500px|frameless|thumb]]<br>
(5) Test Code
<pre>
/*
  keyestudio smart motorhome
  lesson 17.1
  2812 led
  http://www.keyestudio.com
*/
#include <Adafruit_NeoPixel.h>
Adafruit_NeoPixel rgb_2812 = Adafruit_NeoPixel(10, 53, NEO_GRB + NEO_KHZ800); //dset the port of 10 pcs lights to 53, with GRB mode
void setup() {
  rgb_2812.begin();
}
void loop () {
  for (int i = 0; i <= 9; i++) {
    rgb_2812.setPixelColor(i,255,0,0);  //the first light shows red color
    rgb_2812.show();                    //show
    delay(50);
  }
  for (int i = 9; i >= 0; i--) {
    rgb_2812.setPixelColor(i, 0,0,0);  //the first light turns off
    rgb_2812.show();                    //show
    delay(50);
  }
  for (int i = 0; i <= 9; i++) {
    rgb_2812.setPixelColor(i,0,255,0);  //the first light shows green color
    rgb_2812.show();                    //show
    delay(50);
  }
  for (int i = 9; i >= 0; i--) {
    rgb_2812.setPixelColor(i, 0,0,0);  //the first light turns off
    rgb_2812.show();                    //display
    delay(50);
  }
  for (int i = 0; i <= 9; i++) {
    rgb_2812.setPixelColor(i,0,0,255);  //the first light shows blue color
    rgb_2812.show();                    //show
    delay(50);
  }
  for (int i = 9; i >= 0; i--) {
    rgb_2812.setPixelColor(i, 0,0,0);  //the first light turns off
    rgb_2812.show();                    //show
    delay(50);
  }
}
</pre>
(6) Test Result:<br>
Upload code and power on. 2812 light strip shows red, green and blue color.<br>
(7) Project Extension:show gradient colour <br>
<pre>
/*
  keyestudio smart motorhome
  lesson 17.2
  2812 led
  http://www.keyestudio.com
*/
#include <Adafruit_NeoPixel.h>
Adafruit_NeoPixel rgb_2812 = Adafruit_NeoPixel(10, 53, NEO_GRB + NEO_KHZ800); //set the port of 10 pcs lights to 53, with GRB mode
void setup() {
  rgb_2812.begin();