Arduino analog IO experimental study notes ⑤

Creative Commons License Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )

1 Introduction

    Remember we are in the first few explain digital IO, and in fact, we live in most signals are analog signals, such as sound and temperature changes. In Arduino, the common voltage of 0 ~ 5v to an analog signal.

1.1 Analog Input Function

    Pins with "A" before the Arduino, numbered analog input pins, the Arduino analog values can be read on these input pins, i.e. magnitude of the voltage read on the input pin.
    ADC having an analog input pin Function (ADC: Analog-to-Digital Converter ADC). It can be identified when it can convert an analog signal externally input digital signal as a chip operation, thereby achieving the function of reading the analog values.

  • Arduino AVR chip using analog input function has 10-bit resolution, means 0 5V 0 voltage will be converted to integer 1023 of FIG.

    Analog input function requires analogRead (pin) function, wherein the pin is the analog value read pin, the pin must be specified analog input pins.

Analog output 1.2

    To use to analogWrite () function to implement the analog output function.

note:

  • This function is not a true analog output value in the sense, but in a special way to achieve the effect of the analog output value, that is, we often hear "PWM" (Pulse Width Modulation Pulse Width Modulation).

    When analogWrite function, output pin is designated a fixed period (about 490 Hz,) through continuous square wave converting high and low levels, high and low levels by varying the proportion of (duty ratio), obtained in each cycle approximate output effect of different voltage.

image

    analogWrite (pin, value): pin, the parameter value of the PWM wave parameters is to be output pin PWM pulse width, the range 0 to 255.
    Most Arduino pin PWM controller will use "~" logo, you can view your own board. Several common board PWM pin:

image

    Introduces the basics well, then start the next experiment.

2. Experimental content

    To achieve two main experiment contents:

  • Fixed breathing light
  • Adjustable breathing light

2.1 Fixed breathing light

Experiment description:
    The LED is connected to the PWM pin, mainly use analog output function of the output voltage analogWrite, respiratory rate is fixed.

Experiment code:

/**
* @Desc  固定呼吸灯
* @author 单片机菜鸟
* @Date  2016/12/8
* 注意:LED的一端接到5V,另外一端接到输出引脚,低电平亮
*       按键是高电平有效
*/

#define LED 9
#define DELAY_TIME 30 //延时时间

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  //从暗到亮,每次亮度值加5,直到最亮
  for(int fadeValue = 0;fadeValue<=255;fadeValue+=5){
     //输出PWM
     analogWrite(LED,fadeValue);
     //延时一下
     delay(DELAY_TIME);  
  }

  //从亮到暗 每次亮度-5直到最暗
  for(int fadeValue=255;fadeValue>=0;fadeValue-=5){
     //输出PWM
     analogWrite(LED,fadeValue);
     //延时一下
     delay(DELAY_TIME);  
  }
}

2.2 adjustable breathing light

Experiment description:
    a fixed breathing light based on an analog voltage by the size of the read (we use an analog input to a voltage dividing potentiometer) to adjust the frequency, and while using analogRead analogWrite function.

Experiment code:

/**
* @Desc  可调呼吸灯
* @author 单片机菜鸟
* @Date  2016/12/8
* 注意:LED的一端接到5V,另外一端接到输出引脚,低电平亮
*       按键是高电平有效
*/

#define LED 9

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  //从暗到亮,每次亮度值加5,直到最亮
  for(int fadeValue = 0;fadeValue<=255;fadeValue+=5){
     //输出PWM
     analogWrite(LED,fadeValue);
     //延时一下
     int delaytime = analogRead(A0)/5;//读取电位器输出电压 用于延时
     delay(delaytime);  
  }

  //从亮到暗 每次亮度-5直到最暗
  for(int fadeValue=255;fadeValue>=0;fadeValue-=5){
     //输出PWM
     analogWrite(LED,fadeValue);
     //延时一下
     int delaytime = analogRead(A0)/5;
     delay(delaytime);  
  }
}

    Of course, the above can be replaced by photoresistor potentiometer or the like so that it can be considered a respiratory breathing light extinguished day night.

3. Summary

    This lesson is relatively simple, we mainly learn to use analog IO ports. Later when we encounter photosensitive resistors, thermistors, temperature sensors, gas sensors and the like may generate an analog signal, the analog IO port should think of.

Guess you like

Origin blog.csdn.net/dpjcn1990/article/details/92831417