ArduinoIO

1 Potentiometer Control Servo Rotation Angle

This experiment combines a potentiometer with a servo motor, using the potentiometer to control the servo motor. The servo motor rotates to the corresponding angle as the potentiometer knob is turned.

Hardware Connection

  1. Potentiometer:

The potentiometer is a non-polar device and a type of variable resistor. It has three terminals, and by rotating the knob, the position of terminal 2 changes, which in turn changes the resistance value. Terminal 1 is connected to the 5V pin of the development board, terminal 3 is connected to the GND pin, and terminal 2 is connected to an analog input pin.

The connection of the potentiometer’s three terminals is shown below:

  1. Servo Motor

(1) The SG90 servo motor is a position (angle) servo driver, suitable for control systems that require continuous and controllable angle changes. In robot mechatronic control systems, servo motor performance is an important influencing factor. Servo motors can be used as basic output actuators in micro-electromechanical systems (MEMS) and aero modeling, and their simple control and output make it easy to interface with microcontroller systems.

(2) Principle of the Servo Motor: The control signal enters the signal modulation chip from the receiver’s channel, obtaining a DC bias voltage. It has an internal reference circuit that generates a reference signal with a period of 20ms and a width of 1.5ms. The obtained DC bias voltage is compared with the voltage of the potentiometer to obtain the voltage difference output. Finally, the polarity of the voltage difference is output to the motor driver chip to determine the motor’s forward or reverse rotation. When the motor speed is constant, the potentiometer rotates by cascading reduction gears, causing the voltage difference to be zero and the motor to stop rotating. Of course, we don’t need to understand its specific working principle, knowing its control principle is enough. Just like using a transistor, knowing that it can be used as a switch or amplifier, we don’t need to consider how the electrons flow inside the transistor.
The servo motor wiring diagram is shown in the following image:

Hardware Connection

For this experiment, we used an Arduino Uno, a potentiometer, a servo motor, and several wires. The circuit connection diagram is shown below:

The demonstration of this experiment is as follows:

Arduino Code

The code for this experiment is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <Servo.h>           // 声明调用Servo.h库
Servo myservo; // 创建一个舵机对象

int potpin = 0; // 连接到模拟口0
int val; //变量val用来存储从模拟口0读到的值

void setup() {
myservo.attach(11); // 将引脚11上的舵机与声明的舵机对象连接起来
}

void loop() {
val = analogRead(potpin); //从模拟口0读值,并通过val记录
val = map(val, 0, 1023, 0, 179); //通过map函数进行数值转换
myservo.write(val); // 给舵机写入角度
delay(20); // 延时20ms让舵机转到指定位置
}

2 Displaying Distance Data from Ultrasonic Sensor on LCD Screen

This experiment combines an ultrasonic sensor with an LCD screen to measure the distance from the module to obstacles, and display this distance on the LCD screen.

Hardware Connection

  1. Ultrasonic Sensor

(1) The principle of ultrasonic distance measurement is based on the time difference between the transmitter emitting ultrasonic waves and the receiver receiving them, similar to the principle of radar ranging. The ultrasonic transmitter emits ultrasonic waves in a certain direction and starts timing at the moment of emission. The ultrasonic waves propagate through the air and immediately return when they encounter an obstacle. The ultrasonic receiver stops timing when it receives the reflected wave.

(2) Measurement principle:
a. Using a timer, when the echo pin of our ultrasonic module transitions from low to high, we start the timer.
b. When the echo pin of our ultrasonic module transitions back to low, it indicates the end, i.e., stop the timer.
c. Applying the formula: Distance = (Speed (340m/s) * Time) / 2
The ultrasonic sensor pinout is shown in the following image:

  1. LCD Screen

The LCD screen is one of the simplest devices used to display the output of Arduino projects. There are two different types of LCDs: graphic and character dot matrix LCDs. We will be using a character LCD here. The pinout of the LCD screen is as follows:

  1. For this experiment, we will be using an Arduino Uno, an LCD screen, an ultrasonic sensor, and several wires. The circuit connection diagram is shown below:

    The demonstration of this experiment is as follows:

Arduino Code

The code for this experiment is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <LiquidCrystal.h>
#define Trig 8
#define Echo 9
float cm;
float temp;
LiquidCrystal lcd(12,11,5,4,3,2);
int val = 0;

void setup()
{
lcd.begin(16,2);
lcd.print("Welcome to use!");
delay(1000);
lcd.clear();
Serial.begin(9600);
pinMode(Trig, OUTPUT);
pinMode(Echo, INPUT);
}

void loop()
{
digitalWrite(Trig, LOW);
delayMicroseconds(2);
digitalWrite(Trig,HIGH);
delayMicroseconds(10);
digitalWrite(Trig, LOW);
temp = float(pulseIn(Echo, HIGH));
cm = (temp * 17 )/1000;

lcd.setCursor(0,0);
lcd.print("Now Distance:");
lcd.setCursor(2,1);
lcd.print(cm);
lcd.print("cm");
delay(1000);
}

3 Controlling 8x8 Common Cathode Red LED Matrix with Arduino

In this experiment, an 8x8 common cathode red LED matrix is used. The first part of the experiment involves displaying a small heart shape on the screen using code. The second part displays text and a blinking heart on the screen.

Hardware Connection

  1. 8x8 Common Cathode Red LED Matrix

The red LED matrix has a total of 16 pins. These pins represent 8 columns and 8 rows. By specifying which column and row pins have a high or low voltage level, you can control which LEDs are lit up. For example, to light up the top-left LED, you need to set the column pin 13 to a high voltage level and the row pin 9 to a low voltage level. You can use a multimeter to test the circuit continuity and verify the LED lighting.

The schematic diagram and circuit connection of the components are shown in the following figures:

  1. For this experiment, you will need an Arduino Uno, an 8x8 common cathode red LED matrix, a breadboard, and several jumper wires. The hardware connection is shown in the following figure (the component is not available in Tinkercad):

Arduino Code

  1. The first experiment is to light up a heart on the screen. The actual effect is shown below:

The code for this experiment is shown below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
int col[8] = {7,2,A0,4,12,A1,11,A3};
int row[8] = {3,A5,A4,6,A2,5,1,0};
int appear[8][8] = {{0,0,0,0,0,0,0,0},//爱心
{0,1,0,0,0,0,1,0},
{1,0,1,0,0,1,0,1},
{1,0,0,1,1,0,0,1},
{0,1,0,0,0,0,1,0},
{0,0,1,0,0,1,0,0},
{0,0,0,1,1,0,0,0},
{0,0,0,0,0,0,0,0}};
void setup() {
for(int i = 0;i < 8;i++){
pinMode(row[i],OUTPUT);
pinMode(col[i],OUTPUT);
digitalWrite(row[i],LOW);//把行设为高电压,把列设为低电压,避免接通就亮。
digitalWrite(col[i],HIGH);
}
}
void loop() {
draw();
}

void draw(){
for(int i = 0;i < 8;i++){
for(int j = 0;j < 8;j++){
if(appear[i][j] == 1){//逐行逐列检查是否等于1。
digitalWrite(col[i],LOW);//从数组为1的位置开始点亮
digitalWrite(row[j],HIGH);
delay(1);
digitalWrite(row[j],LOW);//把数组为1后面位置都LED熄灭
digitalWrite(col[i],HIGH);
}
}
}
}
  1. The second experiment is to display the text “祝大家节日快乐” (Wishing everyone a happy holiday) on the screen, along with a blinking heart. The actual effect is shown below:

The code for this experiment is shown below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
int col[8] = {7,2,A0,4,12,A1,11,A3};
int row[8] = {3,A5,A4,6,A2,5,1,0};
unsigned char zhu[8][8] = //祝
{
1,0,0,1,1,1,1,1,
0,1,0,1,0,0,0,1,
1,1,1,1,1,1,1,1,
0,1,1,0,1,0,1,0,
1,1,0,0,1,0,1,0,
0,1,0,0,1,0,1,0,
0,1,0,0,1,0,1,0,
0,1,0,1,0,0,1,1,
};
unsigned char da[8][8] = //大
{
0,0,0,1,0,0,0,0,
0,0,0,1,0,0,0,0,
1,1,1,1,1,1,1,1,
0,0,0,1,0,0,0,0,
0,0,0,1,0,0,0,0,
0,0,1,0,1,0,0,0,
0,1,0,0,0,1,0,0,
1,0,0,0,0,0,1,1,
};
unsigned char jia[8][8] = //家
{
0,0,0,1,0,0,0,0,
1,1,1,1,1,1,1,1,
1,0,1,1,1,1,0,1,
0,0,1,1,0,0,1,0,
1,1,1,1,1,1,0,0,
0,1,1,1,1,0,0,0,
1,1,0,1,0,1,0,0,
0,0,1,1,0,0,1,1,
};
unsigned char jie[8][8] = //节
{
0,0,1,0,0,1,0,0,
0,1,1,1,1,1,1,0,
0,0,1,0,0,1,0,0,
0,0,0,0,0,0,0,0,
0,1,1,1,1,1,1,0,
0,0,0,1,0,0,1,0,
0,0,0,1,0,1,1,0,
0,0,0,1,0,0,0,0,
};
unsigned char ri[8][8] = //日
{
0,0,0,0,0,0,0,0,
0,1,1,1,1,1,1,0,
0,1,0,0,0,0,1,0,
0,1,1,1,1,1,1,0,
0,1,0,0,0,0,1,0,
0,1,0,0,0,0,1,0,
0,1,1,1,1,1,1,0,
0,0,0,0,0,0,0,0,
};
unsigned char kuai[8][8] = //快
{
0,1,0,0,1,0,0,0,
0,1,0,0,1,0,0,0,
1,1,0,1,1,1,1,0,
0,1,1,0,1,0,1,0,
0,1,1,1,1,1,1,1,
0,1,0,0,1,0,0,0,
0,1,0,1,0,1,0,0,
0,1,1,0,0,0,1,1,
};
unsigned char le[8][8] = //乐
{
0,0,0,0,0,0,0,1,
0,1,1,1,1,1,1,0,
0,1,0,0,1,0,0,0,
0,1,1,1,1,1,1,1,
0,0,0,0,1,0,0,0,
0,0,1,0,1,0,1,0,
0,1,0,0,1,0,0,1,
0,0,0,1,1,0,0,0,
};
unsigned char ximie[8][8] = //全部熄灭
{
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
};

unsigned char biglove[8][8] = //大“心型”的数据
{
0,0,0,0,0,0,0,0,
0,1,1,0,0,1,1,0,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
0,1,1,1,1,1,1,0,
0,0,1,1,1,1,0,0,
0,0,0,1,1,0,0,0,
};

unsigned char smalllove[8][8] = //小“心型”的数据
{
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,1,0,0,1,0,0,
0,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,0,
0,0,1,1,1,1,0,0,
0,0,0,1,1,0,0,0,
0,0,0,0,0,0,0,0,
};

void setup() {
for(int i = 0;i < 8;i++){
pinMode(row[i],OUTPUT);
pinMode(col[i],OUTPUT);
digitalWrite(row[i],LOW);//把行设为高电压,把列设为低电压,避免接通就亮。
digitalWrite(col[i],HIGH);
}

}

void Clear(){
for(int i = 0;i < 8;i++){
digitalWrite(row[i],LOW);//把行设为高电压,把列设为低电压,避免接通就亮。
digitalWrite(col[i],HIGH);
}
}
void Display(unsigned char dat[8][8]) //显示函数
{
for(int c = 0; c<8;c++)
{
digitalWrite(col[c],LOW);//选通第行

//循环
for(int r = 0;r<8;r++)
{
digitalWrite(row[r],dat[c][r]);
}
delay(1);
Clear(); //清空显示去除余晖
}
}
void BlessYou()
{
for(int i = 0 ; i < 50 ; i++) //循环显示50次
{
Display(zhu); //显示 祝
}
for(int i = 0 ; i < 50 ; i++) //循环显示50次
{
Display(da); //显示 大
}
for(int i = 0 ; i < 50 ; i++) //循环显示50次
{
Display(jia); //显示 家
}
for(int i = 0 ; i < 50 ; i++) //循环显示50次
{
Display(jie); //显示 节
}
for(int i = 0 ; i < 50 ; i++) //循环显示50次
{
Display(ri); //显示 日
}
for(int i = 0 ; i < 50 ; i++) //循环显示50次
{
Display(kuai); //显示 快
}
for(int i = 0 ; i < 50 ; i++) //循环显示50次
{
Display(le); //显示 乐
}
for(int i = 0 ; i < 30 ; i++) //循环显示30次
{
Display(biglove); //显示 大心
}
for(int i = 0 ; i < 30 ; i++) //循环显示30次
{
Display(smalllove); //显示 小心
}
for(int i = 0 ; i < 30 ; i++) //循环显示30次
{
Display(biglove); //显示 大心
}
for(int i = 0 ; i < 30 ; i++) //循环显示30次
{
Display(smalllove); //显示 小心
}
for(int i = 0 ; i < 200 ; i++) //循环显示200次
{
Display(ximie); //显示 熄灭200次
}
}

void loop() {
// put your main code here, to run repeatedly:
BlessYou();
}

Reference article link for this section:

https://blog.csdn.net/m0_37738838/article/details/121683700


ArduinoIO
https://nexmaker-fab.github.io/2023zjude-10-36/2023/12/31/ArduinoIO/
Author
Chenye Meng
Posted on
December 31, 2023
Licensed under