Author: Chen Shuaihua – Exploring the beauty of technical art and Sinology

The original link: www.shuaihuajun.com/article/ard…

Recently, WHENEVER I have time, I have been studying Arduino development boards and realized some superficial entry-level gains. People (at least I am) will erase the trivial memory of things that are not 100% familiar after a certain period of time. The complete memory is corrupted and blurred, so that it cannot be regarded as memory itself only by incomplete memory plus self-righteous conjecture. If you can let the knowledge learned into the bone marrow that is the best, if you can’t expect this, then let the memory become easier.

Remember an LED flow light experiment.

Based on review of

Before introducing LED water lamp, let’s review the knowledge learned. My understanding of digital signals and analog signals is a little deeper, but I’m not sure yet. I just have a feeling, and I sum it up and match some small illustrations for quick recall.

Arduino UNO development board

A digital signal

I used to hear that digital signals were zeros and ones. But more accurately, digital signals are on and off circuits. There are some statements in Arduino programming (the following code snippet is just a list of statements and cannot be put directly into the program) :

pinMode(2, OUTPUT);
pinMode(6, OUTPUT);

digitalWrite(2, HIGH);
digitalWrite(6, LOW);
Copy the code

The meaning of digitalWrite(2, HIGH) statement is to set pin 2 of the Arduino development board to HIGH level. The meaning of the digitalWrite(6, LOW) statement is to set pin number 6 of the Arduino development board to LOW. Output high means that the pin can output the operating voltage of the Arduino development board (for example, the operating voltage of the Arduino UNO I used is 5V). Output low means that the output voltage of this pin is 0V.

In Arduino programming, HIGH is a constant, which can also be represented by 1; LOW is also a constant, or you can call it 0. So, the above code snippet could be written like this:

pinMode(2, OUTPUT);
pinMode(6, OUTPUT);

digitalWrite(2, 1);
digitalWrite(6, 0);
Copy the code

So, you can say that. Digital signals can be represented in Arduino programming by the numbers 1 and 0, as well as HIGH and LOW. A digital signal in a circuit can be represented on and off the circuit.

Some reference books say that HIGH and LOW are more readable, but I don’t think that’s exactly the case. Sometimes it’s simpler and more straightforward to use ones and zeros, but it’s easier to understand. Admittedly, HIGH and LOW are more semantic than 1 and 0. But sometimes, you go too far and it backfires. It doesn’t matter.

OUTPUT in the code above can also be represented by 1 and 0. So 1’s and 0’s don’t represent digital signals, but digital signals can be represented by 1’s and 0’s in Arduino programming. The positive and the opposite statements are not always true. Use 1 for OUTPUT and 0 for INPUT. The above code could be written like this:

pinMode(2, 1);
pinMode(6, 1);

digitalWrite(2, 1);
digitalWrite(6, 0);
Copy the code

I did a couple of Arduino development experiments. I found that pins whose mode was set to output were used to write digital signals. Pins whose mode is set to input are used to read signals. When the truth is made clear, one takes it for granted.

As written in the code above, pin 2 and pin 6 are set to mode 1 (output mode). Therefore, you can use the digitalWrite() function to write digital signals to pins 2 and 6. Pins set to output mode generate high/low voltage. The output is one current after another, the circuit on and off is the digital signal is changing.

Pins in output mode are set to produce voltage changes, and pins in input mode are set to sense voltage changes.

Pins that sense voltage changes are used to read input digital signals. The input voltage is read using the digitalRead() function in Arduino programming. The digital input pin determines whether the induced voltage is high or low at two cut-off points. According to the reference, take my Arduino UNO development board as an example, the voltage that is sensed below 1.5V is identified as low level, and the voltage that is sensed above 3V is identified as high level. The digitalRead function returns either the digits 1 or 0 to indicate whether the pin is sensing high or low.

Almost all the film and television packaging effects I have seen are good at using symbols such as 1010001001 to express information and digitalization. But there are no ones and zeros in real circuits, only on and off circuits. It’s just a way for people in the media to visually understand.

Digital analog signal

There are two sets of pins on the Arduino development board. A set of pins is used to connect the sensor, can receive the analog input signal of the sensor; Another set of pins is used to connect the circuit, and can receive (input) and send (output) digital signals (mentioned above), can also send (output) digital analog signals, but I have not heard of receiving (input) digital analog signals.

I’m not talking about the sensor analog input, or the digital analog input that I haven’t heard of yet. Here is the first digital analog output signal.

Take my Arduino UNO development board for example. Output digital signal, because the computer can not output analog voltage, high level output 5V, low level output voltage (0V). If you want to generate a voltage between 0 and 5V, simulate different voltage values by controlling the duty cycle of on and off per unit time. This is called PWM (Pulse Width Modulation) in reference materials, referred to as Pulse Width Modulation.

The output mode voltage on the pin can make the LED bright and dark, also can make the motor speed fast and slow. In Arduino programming, using the analogWrite() function to output the analog voltage, pins generate stable square waves. If the Arduino outputs 500 digital signals within 1 second, if all 500 are high level 5V voltage, it is high level 5V voltage; If all are low level 0V voltage, it is low level voltage; If half high level, output 2.5V voltage; And so on.

LED running lights

Need to prepare an Arduino UNO development board; A bread board; Dupont line 7; 220 ohm resistors 6; LED6. If I remember correctly, this should be called a parallel circuit:

One of the constructs used in Arduino programming is the for() loop. The complete code is as follows:

void setup() {
  Serial.begin(9600);
  for(int i=2; i<8; i++){
    pinMode(i, 1);
  }
}

void loop() {
  for(int i=2; i<7; i++){
    digitalWrite(i, 1);
    delay(100);
    digitalWrite(i, 0);
  }
  for(int i=7; i>2; i--){ digitalWrite(i, 1); delay(100); digitalWrite(i, 0); }}Copy the code

The delay() function pauses the corresponding number of milliseconds (explained in the previous article). The pinMode() and digitalWrite() functions used in the above code were introduced in the basic review.

And then the final effect looks like this: