int read_value = 0;
int LED_value = 0;
void setup() {
Serial.begin(115200);
pinMode(A0, INPUT);
pinMode(13, OUTPUT);
}
void loop() {
read_value = analogRead(A0);
Serial.println(read_value);
LED_value = map(read_value,0,1023,0,255);//从0-1023映射到0-255
analogWrite(13,LED_value);
delay(100);
}
//map(value, fromLow, fromHigh, toLow, toHigh)
// value: the number to map.
// fromLow: the lower bound of the value's current range.
// fromHigh: the upper bound of the value's current range.
// toLow: the lower bound of the value's target range.
// toHigh: the upper bound of the value's target range.
//arduino 测电压为什么返回的是0~1023
//因为arduino的ADC精度为10位,所以输入信号范围分为 0-1023
//arduino PWM输出精度为0位,所以输出信号范围为0-255