29 August, 2015

Arduino: print sensor value on the LED display

It is pretty simple to print a value from an analog sensor on the LED display.

For example from a light sensor like here:

Output light sensor value to led display

and when the sensor is out of light:

Output light sensor value to led display (closed)

Electronic components from the scheme above:

LED display is connected with Arduino via these 5 pins:

  • DIN - data in
  • CS - chip select
  • CLK - clock pulse source
  • GND
  • 5 V

DIN, CS and CLK are connected with Arduino via digital pins 7, 6 and 5.

Light sensor has 3 pins:

  • GND
  • 5V
  • Signal

where signal pin is connected with Arduino via the analog pin №1.

This program reads values from the sensor 10 times per second and displays the averaged result:

#include <LedControl.h>

const int DIN_PIN = 7;
const int CS_PIN = 6;
const int CLK_PIN = 5;

const int SENSOR_PIN = 1;

LedControl display = LedControl(DIN_PIN, CLK_PIN, CS_PIN);

void setup() {
  display.clearDisplay(0);
  display.shutdown(0, false);
  display.setIntensity(0, 10);
}

void displayNumber(int number) {
  display.clearDisplay(0);
  for (int i = 0; i < 8; i++) {
    int digit = number % 10;
    number /= 10;
    display.setDigit(0, i, digit, false);
    if (number == 0) {
      break;
    }
  }
}

void loop() {
  int value = 0;
  for (int i = 0; i < 10; i++) {
    value += analogRead(SENSOR_PIN);
    delay(100);
  }
  value /= 10;

  displayNumber(value);
}

No comments: