Go Down

Topic: LDR with 7 seg display and PWM output (Read 13453 times) previous topic - next topic

CrossRoads

I have a board that has a '328P set up like an Uno, with offboard USB/Serial adapter, and 12  shift registers for driving large high voltage displays based on LED strips with built-in current limit resistors.
Could be expanded to 15 digits and add in current limit resistors for 7-segment displays.
http://www.crossroadsfencing.com/BobuinoRev17/
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

cjcj

Sorry about attachment.  I've added it here.  Please do take a look at it.

I took a quick look at the chip (tpic6b595) before - I think I had multiplexing on the brain, hence I assumed it was for 2 digits.  Yes, 8 drains, so 1 digit.  I'll new quite a few of these, but if it saves a stack of MOSFETs, it'll be worth it.

I'd be happy for Bobs help with these chips.

CrossRoads

Yes, TPIC6B595 can handle these currents:
Segments A, D & G = 7 x 8 (rows) of LEDs = 8x15 = 120mA each
Segments B, C, E & F = 7 x 7 (rows) of LEDs = 105mA each

Need one shift register per digit.

15 digits, daisy chain them all and send out 15 bytes when an update is needed.
I often use a data array to hold the value, 0 to 9, for each digit, and a font look up array to map the value to the needed segments.
Code: [Select]

byte dataArray[15]; // 15 bytes holding 0-9 for each digit, time, score, period, whatever

byte fontArray[] = {
0b00111111, // 0, with  DP-g-f-e-d-c-b-a, 1 = segment on
0b00000110, // 1       a
etc.                        f       b
0b01101111, // 9       g
};                          e      c
                                 d     DP

// time for an update?
digitlalWrite (ssPin, LOW); // D10 to RCLK pin, D11 to Ser data, D13 to SRCLK
for (x=0; x<15; x=x+1){
SPI.transfer (fontArray[dataArray[x]]);
}
digitlalWrite (ssPin, HIGH);  // outputs updated on this rising edge
// MRCLR tied High, OE/ tied Low, or to a PWM pin for brightness control
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

cjcj

#33
Mar 28, 2015, 12:35 am Last Edit: Mar 28, 2015, 03:32 am by cjcj
Thanks for that info Bob and I like the look of your board - I'll aim to make mine similar.  I'm feeling more confident now and getting excited for my build!  I've found the following:
  • 15 x TPIC6B595N from RS components @ $2.30 (AU) each - LINK_1 - assuming "N" is appropriate???
  • 15 x 20 way IC DIP sockets @ $1.75 (AU) each - LINK - I assume these are suitable too?
  • Headers and decoupling capacitors - I have some already
  • Printed circuit board - time to start drawing, printing, then get out the wife's iron again!

Sorry but I now have a few more questions if I can:

  • I have 5 functions to run - a 2 digits home score, 2 digit away score, 4 digit count down timer (MM:SS), clock (HH:MM) and temperature.  Should I run each of these with a separate Arduino board.  I think I need 45 pins for data,latch and clock, plus I'll need some for operations such as reset scores, advance time, start timer, etc, etc
  • If I write separate code for each of the 5 functions above (work in progress), and maybe your suggestion is to use less boards (such as 2 x Mega), then is it possible (or even advisable) to merge the code?
  • Similar to your board above however, I too would love to "build" the arduino setup as well - not that I know how to do that yet.  Your suggestion?
  • On my current setup, I'm using a 12 channel remote control to operate the board LINK.  Could I instead build something similar using arduino?
  • I also assume on my board I'll have to build a 5V supply (with a voltage regulator).  Again is this advisable, or a completely separate supply
  • Lastly, with the PICs, can I still control "dimming" of all displays via an LDR still?  I was originally doing this with a PWM output on the board

Sorry for the zillion questions, but I'm just learning and this is a big challenge for me.

PaulRB

#34
Mar 28, 2015, 12:41 am Last Edit: Mar 28, 2015, 01:01 am by PaulRB
One Arduino will be fine - Uno/Nano/Pro Mini as you like. You will only need 3 pins to run all 15 shift registers.

If you want to "roll you own" arduino, you could use an atmega328 and regulator circuit. But a Nano for example comes with built-in usb for easy re-programming and a regulator to provide it with 5V (just be careful not to use much current from the 5V supply, or the built-in regulator could overheat, given the 24V input voltage).

You can buy very cheap ir remotes and sensor kits for Arduino on eBay. Also you can find ds3231 rtc modules which have a built-in temperature sensor.

ir remote kit

rtc with temperature sensor

For dimming, you can use an ldr connected to an analog input, and connect an Arduino pwm output to the "OE" pins on the shift registers.


CrossRoads

cjcj, your first 2 links won't open for me.
You can do all 15 digits in one sketch.
Standalono arduino is simple - you can see the parts:
16 MHz crystal, 22pF caps, 0.1uF cap, 10K resistor.
I add ICSP header for bootloading as an Uno, or for Upload Using Programmer after bootloading (bootloading also sets fuses); add FTDI header if you want serial access for debugging (recommended!) and serial downloading after bootloading.

Can read relay closures the same as button presses:
Code: [Select]

pinMode (pinX, INPUT_PULLUP);

if (digitalRead(pinX) == LOW){
// relay closure connects pin to Gnd
// advance a score or something
}


5V supply - how much current is expected? Maybe 5V linear regulator (78005 type), maybe 5V switching regulator from a higher input voltage source, such as
http://www.digikey.com/product-detail/en/OKI-78SR-5%2F1.5-W36-C/811-2196-5-ND/2259781

Use PWM to control brightness, determine value from 255 (full dim) to 0 (full bright) however you'd like, as PaulRB indicated.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

cjcj

Bob, I've tried to fix the first link with a "longer" web address, but couldn't change the 2nd.  Plan B - I've attached a PDF of them both.

Once I've done the code for the 5 functions, I will therefore try to combine them.

Paul - 3 pins to run all 15 registers - thats fantastic, I had no idea I could.  Not sure how to do that yet, but I'll start by taking small steps.  I know that the arduino is all setup with the 5V supply, USB, etc as you say, but I am "tempted" to consider creating that circuitry as well in my board (as Bob suggested).  But again, that will come later.

I did buy an RF trasmitter / receiver module earlier this year to experiment with (link here), but your link with keypad is brilliant.

I also did buy the DS3231 AT24C32 clock (link here).  Just received a battery for it last week, but haven't tried it yet.  I did however write code in a sketch to run my  clock - and I was so proud of doing it.  Is it better I use this module?  Isn't that sort of cheating and relying on pre made components!  And I didn't know it did temperature too - excellent - another bit of research for me / homework.

Dimming - OE pins - also excellent.  This is all sounding so promising.  Chris.

CrossRoads

$2.30 per part - you guys down under get hosed on prices.
Same for me is $1.74
http://www.digikey.com/product-search/en?pv69=80&k=tpic6b595&mnonly=0&newproducts=0&ColumnSort=0&page=1&quantity=0&ptm=0&fid=0&pageSize=25

Quote
3 pins to run all 15 registers - thats fantastic, I had no idea I could.  Not sure how to do that yet,
Did you look at the code I posted in #32?
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

cjcj

Did you look at the code I posted in #32?
I did read it, however I assumed that each register "linked" but needed at least 3 to 4 wires each. 
As for the code, I'm not too hot at the moment reading code.  I loosely understand what you've written in principle, however I have to actually write this up and apply it in context to fully understand it.  I'll order the shift registers, mock up 15 small led displays with it, then write the code to learn how to do it.

Below for instance is an example of the code I've written for a MM:SS timer.  You'll look at it and think it's quite long winded.  But self taught, so far it's the best I can do.

Code: [Select]

/* COMMON ANODE TIMER (with start, pause and reset):
   This Arduino code drives 4 separate 7 segment common ANODE LED displays
   Via a pushbutton switch, it starts a timer display from 00:00
   (mm:ss) to 59:59.  The button also pauses the timer.
   Holding the button in for greater than 2 seconds resets
   There is a built-in debounce to limit bouncing of pulses via the pushbutton.
   Includes an LDR to control the intensity of the digits
   Create by Chris in Feb 2015
*/

//--------------------------------------------------------------------------------------------

int A = 1;                   // Set to 0 (LOW) for common cathode.  Set to 1 for common anode digits
int B = 0;                   // Set to 1 (HIGH) for common cathode.  Set to 0 for common anode digits
int ldrIn = 14;              // Analog input pin A0.  Attach to first leg of LDR and 10K to GND
int aValue = 0;               // A variable used in controlling the light intensity value
int dValue = 0;               // A variable used in controlling the light intensity value

int buttonChange = 15;       // Analog in pin (A1) used to pause, restart and reset timer
                             // Other end of led to GND via a 10K resistor
int state = LOW;             // The current state of the output pin
int reading;                 // The current reading of the input pin
int previous = HIGH;         // The previous reading from the input pin
int check = 0;               // Used to pause the clock when the button is preassed - whilst reset
int delayMp = 2;             // Used to delay the muliplexing
int sec_ones = 0;            // Used to indicate what number the digit is set to
int sec_tens = 0;            // Used to increment the tens unit
int min_ones = 0;            // Used to increment the min_ones units
int min_tens = 0;            // Used to increment the min_tens units
long time1 = 0;              // Used in delaying for debouncing of pushbutton
long time2 = 0;              // Used in delaying the increment clock time
long secondMicro = 996000;   // One second increment clock time
long debounce = 500;         // Debounce time - longer than usual so it works for me
unsigned long currentMillis = 0;  //
unsigned long previousMillis = 0; //
int stepMultiplex = 1;       // Variable used to step through each segment
int reset = 2000;            // Duration required for button press to reset time
int firstTime = 1;           // Used in recording button press duration
unsigned long startTime = 0; // Used in recording button press duration
unsigned long pressTime = 0; // Used in recording button press duration


// the following array is used to define each led common
byte commons [4] = {6,9,10,11};   // pins to each digit common (these are PWM outputs)

// the following array is used to define each led segment for number 0 to 9
byte sevenSegmentPin[7] = {2,3,4,5,7,8,12};
byte sevenSegment[10][7] = {
  {B,B,B,B,B,B,A},    // this is 0
  {A,B,B,A,A,A,A},    // this is 1
  {B,B,A,B,B,A,B},    // this is 2
  {B,B,B,B,A,A,B},    // this is 3
  {A,B,B,A,A,B,B},    // this is 4
  {B,A,B,B,A,B,B},    // this is 5
  {B,A,B,B,B,B,B},    // this is 6
  {B,B,B,A,A,A,A},    // this is 7
  {B,B,B,B,B,B,B},    // this is 8
  {B,B,B,A,A,B,B},    // this is 9
}; 


void setup()  //--------------------------------------------------
{
  Serial.begin(9600);
  for (byte i=0; i<7; i++)
  pinMode(sevenSegmentPin[i],OUTPUT); // this sets pins 2,3,4,5,6,8 and 12 as outputs

  for(byte i=0; i<4; i++)
  pinMode(commons[i], OUTPUT);         // this sets pins 6,9, 10 and 11 as outputs

  for (byte i=0; i<7; i++) {
    digitalWrite(sevenSegmentPin[i], sevenSegment[0][i]); // this sets pins 2,3,4,5,6 and 8 as 0
  }
}

// This is code for displaying the incremental digit for the "sec_ones"
void sevenSegWrite_1(byte sec_ones) {
  for (byte i=0; i<7; i++) {
    digitalWrite(sevenSegmentPin[i], sevenSegment[sec_ones][i]);
  }
}

// This is code for displaying the incremental digit for the "sec_tens"
void sevenSegWrite_2(byte sec_tens) {
  for (byte i=0; i<7; i++) {
    digitalWrite(sevenSegmentPin[i], sevenSegment[sec_tens][i]);
  }
}

// This is code for displaying the incremental digit for the "min_ones"
void sevenSegWrite_3(byte min_ones) {
  for (byte i=0; i<7; i++) {
    digitalWrite(sevenSegmentPin[i], sevenSegment[min_ones][i]);
  }
}

// This is code for displaying the incremental digit for the "min_tens"
void sevenSegWrite_4(byte min_tens) {
  for (byte i=0; i<7; i++) {
    digitalWrite(sevenSegmentPin[i], sevenSegment[min_tens][i]);
  }
}


void loop()  //--------------------------------------------------
{
  aValue = analogRead(ldrIn);              // Read the LDR analogue value on analog pin and
                                          // store the value between 0 and 1023
//  Serial.print("0l to 1023 value = ");
//  Serial.println(aValue);
  aValue = constrain (aValue, 20, 300);   // Constrain the value between 900 and 1010
  dValue = map (aValue, 20, 300, 0, 255); // The digital pin outputs values from 0 to 255
                                          // so convert the value received in the above line
 
  reading = digitalRead(buttonChange); // Check the state of the button
  currentMillis = millis();            // Set variable for multiplexing timing

 
// Used to toggle starting and pausing the clock...

if (reading == HIGH && previous == LOW && millis() - time1 > debounce) {
    if (state == HIGH) {
      state = LOW;
      time1 = millis ();
    }
    else {
      state = HIGH;
      time1 = millis();
    }
  }
  previous = reading;


// Used to check if the button is pressed long enough to reset the time

 if (reading == HIGH) {
   if (firstTime == 1) {
     startTime = millis();
     firstTime = 0;
   }
   pressTime = millis () - startTime;
 }
 else if (firstTime == 0) {
   firstTime = 1;
 }
 
 if (pressTime > 2000) {
    state = LOW;
    sec_ones=0;
    sec_tens=0;
    min_ones=0;
    min_tens=0;
    time1 = 0;
 }
 
//-------------------------------------------------------------------

  if(stepMultiplex == 1 && (currentMillis - previousMillis) >= delayMp) {
    digitalWrite(commons [0],B);  // Turn off the previous digit once time expires
    previousMillis=currentMillis;
    sevenSegWrite_1(sec_ones);
    analogWrite(commons [3],dValue);  // Turn on the sec_one digit
    stepMultiplex=2;
  }
 
  if(stepMultiplex == 2 && (currentMillis - previousMillis) >= delayMp) {
    digitalWrite(commons [3],B);  // Turn off the previous digit once time expires
    previousMillis=currentMillis;
    sevenSegWrite_2(sec_tens);
    analogWrite(commons [2],dValue);  // Turn on the sec_tens digit
    stepMultiplex=3;
  }
   
  if(stepMultiplex == 3 && (currentMillis - previousMillis) >= delayMp) {
    digitalWrite(commons [2],B);  // Turn off the previous digit once time expires
    previousMillis=currentMillis;
    sevenSegWrite_3(min_ones);
    analogWrite(commons [1],dValue);  // Turn on the min_one digit
    stepMultiplex=4;
  }

  if(stepMultiplex == 4 && (currentMillis - previousMillis) >= delayMp) {
    digitalWrite(commons [1],B);  // Turn off the previous digit once time expires
    previousMillis=currentMillis;
    sevenSegWrite_4(min_tens);
    analogWrite(commons [0],dValue);  // Turn on the min_tens digit
    stepMultiplex=1;
  }

//  The next few lines are used to advance the clock
 
  if (micros() - time2 > secondMicro && state == HIGH) {
    check=1;                            //this is used to pause the clock while the button is pressed
    if (check == 1 & reading == LOW) {
      sec_ones++;
      check=0;
    }
   
    if(sec_ones >9) {
      sec_ones=0;
      sec_tens++;
    }
   
    if(sec_tens>5) {
      sec_tens=0;
      min_ones++;
    }
   
    if(min_ones>9) {
      min_ones=0;
      min_tens++;
    }
   
    if(min_tens>5) {
      sec_ones=0;
      sec_tens=0;
      min_ones=0;
      min_tens=0;
    }
    time2 = micros();
  }
}

PaulRB

I also did buy the DS3231 AT24C32 clock...  I did however write code in a sketch to run my  clock - and I was so proud of doing it.  Is it better I use this module?  Isn't that sort of cheating and relying on pre made components!
Why get a dog and bark yourself? The RTC module has several important advantages over using the Arduino as a clock. It will be much more accurate over long term, Arduino will probably loose/gain several mintues per day, vs several minutes per year with ds3231. RTC has battery back-up, so doesn't need re-setting every time Arduino is powered up/reset.
I'll order the shift registers, mock up 15 small led displays with it, then write the code to learn how to do it.
Make sure to buy common-anode individual/single-digit 7-seg displays for your mock-up. You won't be able to use multi-digit displays without multiplexing them, and that wouldn't be an accurate mock-up of your final curcuit.
Below for instance is an example of the code I've written for a MM:SS timer.  You'll look at it and think it's quite long winded.  But self taught, so far it's the best I can do.
Its good that you are practising your coding skills, but little of that code will be suitable for your project. You won't be multiplexing, you will be using shift registers and an RTC connected to the i2c bus. For now, better to spend your time getting the code for that RTC module working. Just send the output to serial monitor for now.

cjcj

You are right.  I already noticed I had to fiddle with microseconds in order to get the time accurate'sh.  At least I learnt something with my code.  As for the battery backup - yes, I didn't think of that.

I've already got small 7 segment common anode digits (10 at this stage, so I'll order another 1/2 dozen).

I do realize the multiplexing is out the window now.  Again - good practice and I'm learning heaps.

cjcj

#41
Mar 29, 2015, 03:56 pm Last Edit: Mar 29, 2015, 03:59 pm by cjcj
$2.30 per part - you guys down under get hosed on prices.  Same for me is $1.74
Bob, could you check this LINK for shift registers on ebay.  Do you think I should trust them?


CrossRoads

Up to you. I don't trust anything on e-bay. I only by electronics from actual distributors.
If this is just for personal use, that's different.  I deliver a lot of what I buy, so I buy from known good suppliers.
I also can't live with 3-4 week delivery time.

As for the code:
have an array of elements that you will shift out.
Code: [Select]

dataArray[] = {
leftScoreTens,
leftScoreOnes,
rightScoreTens,
rightScoreOnes,
minutesTens,
minutesOnes,
secondsTens,
secondsOnes,
tenths,
//etc
};

Then send the data to the shift registers
Code: [Select]

digitalWrite (ssPin, LOW); // connects to RCLK
for (x=0; x<15; x=x+1){
SPI.transfer (fontArray[dataArray[x]]); // look up font mapping and send it out for data at location x in dataArray
}
digitalWrite (ssPin, HIGH); // connects to RCLK, outputs update on this rising edge.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

cjcj

Up to you. I don't trust anything on e-bay....If this is just for personal use, that's different... I buy from known good suppliers...I also can't live with 3-4 week delivery time.
The 3 to 4 weeks is a pain, but in my it will take me a while to do this project.  In my previous large project I did use ebay components alot of the time, except for anything high powered like the TPIC6B595s.  Yes, this is not for "personal" use, so I might just order 15 of from a local suppler (rs components) and some "spares only" from ebay.  This way I can start breadboarding earlier.

Thanks for all the snipits of code.  They will come in handy when I get my PICs and start coding.

For now, better to spend your time getting the code for that RTC module working. Just send the output to serial monitor for now.
I did try my RTC, the only problem was that it was labelled on the ebay website as "DS3231", however I was sent a "DS1307" (no temperature on it).  The $2 lost on this module is not the problem - only the 3-4 week waiting again!  I connected it and tried some code anyway - time works well (but I've read the earlier DS1307 looses too much time as it is heat affected).  New module on order.

Time for me to read some posts on shift registers and do some more practice...

PaulRB

it was labelled on the ebay website as "DS3231", however I was sent a "DS1307"
On eBay? I hope you opened a dispute!

Go Up