我最近得到了一个 Arduino Uno Wifi(用Wifilink 固件刷新它以支持 UDP),并且我创建了一个简单的草图来从 flex 传感器读取模拟值。
我正在使用下面的代码(从官方网站复制粘贴)
const int FLEX_PIN = A4; // Pin connected to voltage divider output
// Measure the voltage at 5V and the actual resistance of your
// 47k resistor, and enter them below:
const float VCC = 4.98; // Measured voltage of Ardunio 5V line
const float R_DIV = 47500.0; // Measured resistance of 3.3k resistor
// Upload the code, then try to adjust these values to more
// accurately calculate bend degree.
const float STRAIGHT_RESISTANCE = 37300.0; // resistance when straight
const float BEND_RESISTANCE = 90000.0; // resistance at 90 deg
void setup() {
Serial.begin(9600);
pinMode(FLEX_PIN, INPUT);
}
void loop() {
// Read the ADC, and calculate voltage and resistance from it
int flexADC = analogRead(FLEX_PIN);
float flexV = flexADC * VCC / 1023.0;
float flexR = R_DIV * (VCC / flexV - 1.0);
Serial.println("Resistance: " + String(flexR) + " ohms");
// Use the calculated resistance to estimate the sensor's
// bend angle:
float angle = map(flexR, STRAIGHT_RESISTANCE, BEND_RESISTANCE, 0, 90.0);
Serial.println("Bend: " + String(angle) + " degrees");
Serial.println();
delay(500);
}
该代码在 Arduino Uno Wifi 中不起作用:它没有显示弯曲度的任何变化。但是,它适用于我用于测试的另一个 Arduino Uno。
我还做了一个测试,发现 Arduino Uno Wifi 中的引脚 4 和 5 出于某种原因即使使用空脚本也会产生电流,能够点亮 LED(而引脚 0、1、2、3 不点亮) .
这同样适用于传感器的值。例如,如果我将柔性传感器放在模拟引脚 0 中,那么当我弯曲它时值会发生变化(例如 362 读取不弯曲和 600 弯曲,这很好)。
另一方面,当我将传感器的引脚放入 A4 或 A5 时,从 1023(起始值)变为 982,无论我弯曲传感器,它都不会改变。
我最近得知这些引脚是 Wire/TWI/I2C 引脚。有什么办法可以从这些引脚读取模拟输入?
也发布在Arduino 论坛链接