Devices & Components
Arduino Uno Rev3
LDR, 5 Mohm
Resistor 220 ohm
Resistor 10k ohm
74hc595 IC
7 Segment LED Display, Red
Project description
Code
Luxmeter
c_cpp
if you are in an environment that light may change a lot do reconsider the temporisation values of delay function.
1//LostGhost-2020.06.03 2 3int latch=3; //74HC595 pin 9 STCP 4int clock=4; //74HC595 pin 10 SHCP 5int data=2; //74HC595 pin 8 DS 6 7unsigned char table[]= 8{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c 9,0x39,0x5e,0x79,0x71,0x00};//Display digital data 10 11const int nbrDigits= 4; // the number of digits in the LED display 12 //dig 1 2 3 4 13const int digitPins[nbrDigits] = { 5,6,7,8}; 14 15int lightPin = 0; //LDR 16 17void setup() { 18 //Configuring output pins for the 74HC595 19 pinMode(latch,OUTPUT); 20 pinMode(clock,OUTPUT); 21 pinMode(data,OUTPUT); 22 23 //Configuring output pins for commun cathod of each 7 segment digit 24 for(int i=0; i < nbrDigits; i++) 25 { 26 pinMode(digitPins[i], OUTPUT); 27 digitalWrite(digitPins[i],HIGH); 28 } 29} 30 31void loop() { 32 int digitalReading = analogRead(lightPin); 33 DisplayNumber(digitalReading); 34 delay(1); 35} 36 37//Function to display a nmuber on 4 digit 7 segments (maximum 9999) 38void DisplayNumber( int number) { 39 if(number == 0) 40 DisplayDigit( 0, nbrDigits-1) ; // display 0 in the rightmost digit 41 else 42 { 43 // display the value corresponding to each digit 44 // leftmost digit is 0, rightmost is one less than the number of places 45 for( int digit = nbrDigits-1; digit >= 0; digit--) 46 { 47 if(number > 0) 48 { 49 DisplayDigit( number % 10, digit) ; 50 number = number / 10; 51 } 52 } 53 } 54} 55 56//Function to display on one seven segments digit 57void DisplayDigit(unsigned char num, int digit) { 58 digitalWrite(latch,LOW); 59 shiftOut(data,clock,MSBFIRST,table[num]); 60 digitalWrite(latch,HIGH); 61 62 digitalWrite( digitPins[digit], LOW ); 63 delay(4); 64 digitalWrite( digitPins[digit], HIGH ); 65}
Downloadable files
wiringdaiagram_YmowgxDyNw.PNG
Wiring diagram
wiringdaiagram_YmowgxDyNw.PNG
wiringdaiagram_YmowgxDyNw.PNG
Wiring diagram
wiringdaiagram_YmowgxDyNw.PNG
Comments
Only logged in users can leave comments