Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 7 of 7

Thread: Teensy 3.2 and 74HC595 Problems

  1. #1

    Teensy 3.2 and 74HC595 Problems

    Hi all, its my first time ever using 74hc595s with the teensy, and I'm going to be working on a bar graph type project. So I have four 595s hooked up to the teensy and wanted to try the library 'Shifty' to make things a bit easier for me. But I cant get any leds to turn on or off. Ive tried a sketch just to test the shift registers but haven't had any luck. Here's my schematic https://imgur.com/gmv0wg7 and here's the program I've tried. I have it all hooked up like this on a circuit board I made.

    Code:
    #include <Shifty.h>
    
    // Declare the shift register
    Shifty shift; 
    
    void setup() {
      // Set the number of bits you have (multiples of 8)
      shift.setBitCount(32);
    
      // Set the clock, data, and latch pins you are using
      // This also sets the pinMode for these pins
      shift.setPins(13, 11, 8); 
      pinMode(21, OUTPUT);
    }
    
    void loop() {
      // writeBit works just like digitalWrite
      shift.writeBit(1, HIGH);
      digitalWrite(21, HIGH);
      delay(500);
      shift.writeBit(3, HIGH);
      delay(500);
      shift.writeBit(1, LOW);
      digitalWrite(21, LOW);
      delay(500);
      shift.writeBit(3, LOW);
     
      delay(500);
    }
    In this code I added the digitalWrite(21, HIGH); and digitalWrite(21, LOW); just to make sure the teensy was working in this setup and that led does blink but nothing on the bar graph blinks.

    I'm also curious as to which pins need to be used for clock latch and data. Do I have to use 13, 11 and 8? im going off of this site. https://www.pjrc.com/teensy/td_libs_ShiftPWM.html
    Any help tips would be appreciated, Thanks.

  2. #2
    Playing around with this code i can send a number through the serial port and get leds to light so i know the shift registers are working.
    Code:
    const int latchPin = 8;
    const int clockPin = 13;
    const int dataPin = 11;
    
    void setup() {
    pinMode(latchPin, OUTPUT);
    pinMode(dataPin, OUTPUT);
    pinMode(clockPin, OUTPUT);
    Serial.begin(9600);
    Serial.println("reset");
    }
    
    void loop() {
    if (Serial.available() > 0) {
    int bitToSet = Serial.read() - 48;
    char buf[100];
    sprintf(buf,"%d\n",bitToSet);
    Serial.write(buf);
    registerWrite(bitToSet, HIGH);
    }
    }
    
    void registerWrite(int whichPin, int whichState) {
    byte bitsToSend = 0;
    
    digitalWrite(latchPin, LOW);
    
    bitWrite(bitsToSend, whichPin, whichState);
    
    shiftOut(dataPin, clockPin, MSBFIRST, bitsToSend);
    
    digitalWrite(latchPin, HIGH);
    
    }
    Strangely when I go back to the previous code, what ever leds I left on start scrolling to the right until no leds are left on and then nothing happens.
    Last edited by cfredisded; 11-11-2017 at 09:04 PM.

  3. #3
    Senior Member+ Theremingenieur's Avatar
    Join Date
    Feb 2014
    Location
    Colmar, France
    Posts
    2,442
    I never heard of that shifty library before. Could it be that it was written with specific hardware register access for AVR CPUs, common in the Arduino world? That would explain why it doesn‘t work on the more modern ARM CPUs on which the Teensys are based. Perhaps you could provide a link?

  4. #4
    Quote Originally Posted by Theremingenieur View Post
    I never heard of that shifty library before. Could it be that it was written with specific hardware register access for AVR CPUs, common in the Arduino world? That would explain why it doesn‘t work on the more modern ARM CPUs on which the Teensys are based. Perhaps you could provide a link?
    Here's a link to the library. https://github.com/johnnyb/Shifty
    I just found it searching '595' in the arduino libraries. I'm trying to eventually do something like this but with 32 led bar graph.
    Code:
    /*
      LED bar graph
    
      Turns on a series of LEDs based on the value of an analog sensor.
      This is a simple way to make a bar graph display. Though this graph uses 10
      LEDs, you can use any number by changing the LED count and the pins in the
      array.
    
      This method can be used to control any series of digital outputs that depends
      on an analog input.
    
      The circuit:
      - LEDs from pins 2 through 11 to ground
    
      created 4 Sep 2010
      by Tom Igoe
    
      This example code is in the public domain.
    
      http://www.arduino.cc/en/Tutorial/BarGraph
    */
    
    // these constants won't change:
    const int analogPin = A0;   // the pin that the potentiometer is attached to
    const int ledCount = 10;    // the number of LEDs in the bar graph
    
    int ledPins[] = {
      2, 3, 4, 5, 6, 7, 8, 9, 10, 11
    };   // an array of pin numbers to which LEDs are attached
    
    
    void setup() {
      // loop over the pin array and set them all to output:
      for (int thisLed = 0; thisLed < ledCount; thisLed++) {
        pinMode(ledPins[thisLed], OUTPUT);
      }
    }
    
    void loop() {
      // read the potentiometer:
      int sensorReading = analogRead(analogPin);
      // map the result to a range from 0 to the number of LEDs:
      int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);
    
      // loop over the LED array:
      for (int thisLed = 0; thisLed < ledCount; thisLed++) {
        // if the array element's index is less than ledLevel,
        // turn the pin for this element on:
        if (thisLed < ledLevel) {
          digitalWrite(ledPins[thisLed], HIGH);
        }
        // turn off all pins higher than the ledLevel:
        else {
          digitalWrite(ledPins[thisLed], LOW);
        }
      }
    }
    From https://www.arduino.cc/en/Tutorial/BarGraph

  5. #5
    Senior Member+ Theremingenieur's Avatar
    Join Date
    Feb 2014
    Location
    Colmar, France
    Posts
    2,442
    I had a look at the library’s source code. It uses specific internal Arduino 8bit functions like bitSet(), bitRead(), etc. I’m not sure if these are intended to work correctly in 32bit Teensyduino. You’d have to write test code and checking the result, or better write your own library with optimized Teensy code.

  6. #6
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    2,001
    On a T3.2, I hooked up a 74hc595 powered with 3.3v with a few LEDs. For data,clock,latch I used 2,3,4. Using shiftOut() everything behaved as expected. And everything still worked using the Shifty library. I didn't daisy-chain, but setting to 32 bits in Shifty, and turning on 24 and 25, it would toggle LED 0 and 1, as expected. Maybe you could do some simple breadboard tests. I didn't examine your schematic carefully.

    I would just use shiftOut(). arduino has some daisy-chaining examples https://www.arduino.cc/en/Tutorial/ShiftOut


    EDIT: Note, the comments in your Shifty sketch in post #1 suggest the wrong pin ordering, here is what Shifty.h says
    void setPins(int dataPin, int clockPin, int latchPin);
    Last edited by manitou; 11-13-2017 at 04:59 PM.

  7. #7
    Quote Originally Posted by manitou View Post
    On a T3.2, I hooked up a 74hc595 powered with 3.3v with a few LEDs. For data,clock,latch I used 2,3,4. Using shiftOut() everything behaved as expected. And everything still worked using the Shifty library. I didn't daisy-chain, but setting to 32 bits in Shifty, and turning on 24 and 25, it would toggle LED 0 and 1, as expected. Maybe you could do some simple breadboard tests. I didn't examine your schematic carefully.

    I would just use shiftOut(). arduino has some daisy-chaining examples https://www.arduino.cc/en/Tutorial/ShiftOut


    EDIT: Note, the comments in your Shifty sketch in post #1 suggest the wrong pin ordering, here is what Shifty.h says
    void setPins(int dataPin, int clockPin, int latchPin);
    Thank you very much! Yes indeed in the shifty example code the comment says..
    Code:
     // Set the clock, data, and latch pins you are using
    When it should say...
    Code:
     // Set the data, clock, and latch pins you are using
    I switched those and now it seems to be working more like it should. Thanks again that would have taken me ages

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •