"Arduino Official Tutorial First series" Sample program 3-5 calibration of analog signals

Source: Internet
Author: User

calibration of analog signals

This example shows you a technique for calibrating a sensor: Read the sensor value for 5 seconds at startup, then look for its maximum and minimum value. The maximum and minimum values after calibration will be useful in the map function.

complete the hardware device required for this example
    • Arduino Board or Genuino board
    • LED Analog signal sensor (photoresistors on OK)
    • 10kω Resistor
    • 220ω Resistor
    • Jumper
    • Bread Plate
    • DuPont Line/Bread plate Line
Circuit Construction


The analog signal sensor (e.g. potentiometer/light sensor, etc.) is connected to the a2,led to the 9th pin.

Use the 220ω current limiting resistor to series LEDs to the 9th pin. Connect the photoresistors one foot to the 5V, the other foot to the A0, and add a 10kω pull-down resistor.

schematic diagram

Routine Code

Before setup, you should define the initial maximum minimum value like this:

int1023;        // 最小值int0;           // 最大值

This approach may seem a bit rudimentary, so let's learn it.
At the beginning, you set a minimum initial value. If you read any value that is less than it, update the minimum value with this smaller number. Similarly, at first, you set a maximum initial value, and if you read it to any large value, update the maximum value with this larger number. Write like this:

while5000) {   sensorValue = analogRead(sensorPin);   // 记录传感器最大值   if (sensorValue > sensorMax) {     sensorMax = sensorValue;   }   // 记录传感器最小值   if (sensorValue < sensorMin) {     sensorMin = sensorValue;   } }

In this way, the value of the sensor can be successfully mapped to a range (because the maximum and minimum values are determined):

// 将校准结果应用到传感器读取中=map0255);

Here's the whole code:

/ * Calibration of analog signals shows a way to calibrate the sensor. The maximum and minimum value of the sensor value is found five seconds before the program executes. This approach may seem a bit rudimentary, so let's learn it. At first you set the initial value of the maximum and minimum values, and then look for smaller values and larger values during execution, and constantly update the values you set at the beginning. Circuit CONSTRUCTION: * Analog signal sensor (Photoresistors can) connected to A0 * LED connection 9th pin to GND code is public. *///constants:Const intSensorpin = A0;//Sensor PINConst intLedpin =9;//LED PIN//variables:intSensorvalue =0;//Sensor valueintSensormin =1023;//min valueintSensormax =0;//Maximum valuevoidSetup () {//Turn on led to indicate start of calibration:Pinmode ( -, OUTPUT); Digitalwrite ( -, high);//Calibration in the first five seconds   while(Millis () < the) {Sensorvalue = Analogread (Sensorpin);//Record maximum value    if(Sensorvalue > Sensormax)    {Sensormax = Sensorvalue; }//Record minimum value    if(Sensorvalue < Sensormin)    {sensormin = Sensorvalue; }  }//Off LED indicates calibration completeDigitalwrite ( -, low);}voidLoop () {//Read sensor:Sensorvalue = Analogread (Sensorpin);//The maximum minimum value after calibration is used:Sensorvalue =Map(Sensorvalue, Sensormin, Sensormax,0,255);//Prevent Sensorvalue from exceeding the rangeSensorvalue = Constrain (Sensorvalue,0,255);//Use calibrated values to dimmer the LEDs:Analogwrite (Ledpin, sensorvalue);}

Related information

while ()
Millis ()
Constrain ()
Map ()
If
Analog signal and serial port: read a value from the analog input pin and use this value to control the LED light
Analog signal Input: Use a potentiometer to control the blinking of the LEDs.
Analog output [Mega]: Use the Arduino Mega board to allow 12 LEDs to fade in one after the other.
Analog signal Calibration: Set the maximum and minimum value of the sensor output analog signal to achieve the desired effect.
LED brightness fade: Use the pulse width pin (PWM pin) to weaken the brightness of an LED.
Signal smoothing: Enables more uniform and smooth input values for multiple analog pins.

Update process
Date Log Translator
May 10, 2016 Create and translate documents Tang Jiaxun

Translator: Tang Jiaxun
Email: [Email protected]
qq:649196486
Reproduced please specify the source, all because of love.
I hope you can point out your valuable comments, thank you.

"Arduino Official Tutorial First series" Sample program 3-5 calibration of analog signals

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.