Arduino. Демонстрация работы аналогового входа

Демонстрация работы аналогового входа Arduino, воспользуемся примером с сайта arduino.cc, так же пример есть в приложении arduino в разделе «Файл — Образцы — 03. Analog — AnalogInput».

Описаниеarduino-demonstratsiya-raboty-analogovogo-vkhoda

При помощи изменения сопротивления потенциометра, подключенного к аналоговому входу, меняем скорость мигания светодиода.

Для реализации потребуется

  1. Arduino;
  2. Отладочная плата;
  3. Потенциометр 10 КОм;
  4. Соединительные провода типа «Папа-Папа»;
  5. Резистор 220 Ом;
  6. Светодиод.

* Можно обойтись встроенным светодиодом, тогда можно исключить пункты 5 и 6.

Реализация

Подключаем провода:

  1. Аналоговый вход A0 подключаем к центральному контакту потенциометра;
  2. 5v и GND подключаем к боковым контактам потенциометра;
  3. К D13 подключить резистор 220 Ом, к резистору подключаем анод светодиода, катод подключаем к выходу GND.

Код можно найти там же, где и примеры.

Код сопровождается исчерпывающими комментариями, но отдельно отмечу: на аналоговый вход подается напряжение от 0 до 5 вольт, которое будет  функцией  analogRead() преобразовано в значение от 0 до 1023, это 1024 шага с изменением 0.0049 Вольт.

/*
  Analog Input
 Demonstrates analog input by reading an analog sensor on analog pin 0 and
 turning on and off a light emitting diode(LED)  connected to digital pin 13.
 The amount of time the LED will be on and off depends on
 the value obtained by analogRead().

 The circuit:
 * Potentiometer attached to analog input 0
 * center pin of the potentiometer to the analog pin
 * one side pin (either one) to ground
 * the other side pin to +5V
 * LED anode (long leg) attached to digital output 13
 * LED cathode (short leg) attached to ground

 * Note: because most Arduinos have a built-in LED attached
 to pin 13 on the board, the LED is optional.


 Created by David Cuartielles
 modified 30 Aug 2011
 By Tom Igoe

 This example code is in the public domain.

 http://arduino.cc/en/Tutorial/AnalogInput

 */

int sensorPin = A0;    // select the input pin for the potentiometer
int ledPin = 13;      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {
  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);
  // turn the ledPin on
  digitalWrite(ledPin, HIGH);
  // stop the program for <sensorValue> milliseconds:
  delay(sensorValue);
  // turn the ledPin off:
  digitalWrite(ledPin, LOW);
  // stop the program for for <sensorValue> milliseconds:
  delay(sensorValue);
}

После выгрузки программы, получаем, что требовалось.

Камера не совсем верно передает частоту мерцания, но видно, что она меняется.

Понравилась статья? Поделиться с друзьями:
Добавить комментарий

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!: