Wednesday, February 5, 2014

Voltage level indicator using Arduino

Instruction:
  1. User changes value of a pot resistor. Reference voltage is 5 v.
  2. Arduino reads the input and converts it as following. 0V is will display a 0 and 5v will display 9 on the seven-segment display.
Design:

Code:
/*Name: Voltage level indicator
potentiometer wiper (middle terminal) connected to analog pin 3
outside leads to ground and +5V
variable to store the value read
*/
int val = 0;          
int analogPin = A0;   
int ret;

int pin_2 = 2;
int pin_3 = 3;
int pin_4 = 4;
int pin_5 = 5;
int pin_6 = 6;
int pin_7 = 7;
int pin_8 = 8;

void displayZero() {
   digitalWrite(pin_2, HIGH);
   digitalWrite(pin_3, HIGH);
   digitalWrite(pin_4, HIGH);
   digitalWrite(pin_5, HIGH);
   digitalWrite(pin_6, HIGH);
   digitalWrite(pin_7, HIGH);
   digitalWrite(pin_8, LOW);
}

void displayOne() {
   digitalWrite(pin_2, LOW);
   digitalWrite(pin_3, HIGH);
   digitalWrite(pin_4, HIGH);
   digitalWrite(pin_5, LOW);
   digitalWrite(pin_6, LOW);
   digitalWrite(pin_7, LOW);
   digitalWrite(pin_8, LOW);
}

void displayTwo() {
   digitalWrite(pin_2, HIGH);
   digitalWrite(pin_3, HIGH);
   digitalWrite(pin_4, LOW);
   digitalWrite(pin_5, HIGH);
   digitalWrite(pin_6, HIGH);
   digitalWrite(pin_7, LOW);
   digitalWrite(pin_8, HIGH);
}

void displayThree() {
   digitalWrite(pin_2, HIGH);
   digitalWrite(pin_3, HIGH);
   digitalWrite(pin_4, HIGH);
   digitalWrite(pin_5, HIGH);
   digitalWrite(pin_6, LOW);
   digitalWrite(pin_7, LOW);
   digitalWrite(pin_8, HIGH); 
}

void displayFour() {
   digitalWrite(pin_2, LOW);
   digitalWrite(pin_3, HIGH);
   digitalWrite(pin_4, HIGH);
   digitalWrite(pin_5, LOW);
   digitalWrite(pin_6, LOW);
   digitalWrite(pin_7, HIGH);
   digitalWrite(pin_8, HIGH);
}

void displayFive() {
   digitalWrite(pin_2, HIGH);
   digitalWrite(pin_3, LOW);
   digitalWrite(pin_4, HIGH);
   digitalWrite(pin_5, HIGH);
   digitalWrite(pin_6, LOW);
   digitalWrite(pin_7, HIGH);
   digitalWrite(pin_8, HIGH);
}

void displaySix() {
   digitalWrite(pin_2, HIGH);
   digitalWrite(pin_3, LOW);
   digitalWrite(pin_4, HIGH);
   digitalWrite(pin_5, HIGH);
   digitalWrite(pin_6, HIGH);
   digitalWrite(pin_7, HIGH);
   digitalWrite(pin_8, HIGH);
}

void displaySeven() {
   digitalWrite(pin_2, HIGH);
   digitalWrite(pin_3, HIGH);
   digitalWrite(pin_4, HIGH);
   digitalWrite(pin_5, LOW);
   digitalWrite(pin_6, LOW);
   digitalWrite(pin_7, LOW);
   digitalWrite(pin_8, LOW);
}

void displayEight() {
   digitalWrite(pin_2, HIGH);
   digitalWrite(pin_3, HIGH);
   digitalWrite(pin_4, HIGH);
   digitalWrite(pin_5, HIGH);
   digitalWrite(pin_6, HIGH);
   digitalWrite(pin_7, HIGH);
   digitalWrite(pin_8, HIGH);
}

void displayNine() {
   digitalWrite(pin_2, HIGH);
   digitalWrite(pin_3, HIGH);
   digitalWrite(pin_4, HIGH);
   digitalWrite(pin_5, LOW);
   digitalWrite(pin_6, LOW);
   digitalWrite(pin_7, HIGH);
   digitalWrite(pin_8, HIGH);
}

void Dec_to_7seg(int x) {
    switch(x) {
       case 0:
            displayZero();
            break;
       case 1:
            displayOne();
            break;
       case 2:
            displayTwo();
            break;
       case 3:
            displayThree();
            break;
       case 4:
            displayFour();
            break;
       case 5:
            displayFive();
            break;
       case 6:
            displaySix();
            break;
       case 7:
            displaySeven();
            break;
       case 8:
            displayEight();
            break;
       case 9:
            displayNine();
            break;
    }
}

void setup(){
  pinMode(pin_2, OUTPUT);
  pinMode(pin_3, OUTPUT);
  pinMode(pin_4, OUTPUT);
  pinMode(pin_5, OUTPUT);
  pinMode(pin_6, OUTPUT);
  pinMode(pin_7, OUTPUT);
  pinMode(pin_8, OUTPUT);
  Serial.begin(9600);          //  setup serial
}

void loop(){
  val = analogRead(analogPin);    // read the input pin
  float voltage = val * (9 / 1023.0);
  ret = (int)voltage; 
  //Serial.println(ret);
  Dec_to_7seg(ret);
}

No comments:

Post a Comment