Hardware and Software SPI version of the LiquidCrystal library

LiquidCrystal SPI for Spark Core

A Hardware and Software SPI version of the LiquidCrystal library for the Spark Core.

GET IT HERE! https://github.com/technobly/SparkCore-LiquidCrystalSPI

Hardware SPI Wiring (default)
wiring

Hardware SPI Example Code (default)

// Create an instance of the library for HARDWARE SPI mode (define SS "latch" pin)
// Default SS pin is A2, but can be ANY of the A0 - A2, A6, A7 or D0 - D7 pins.
// Just make sure you don't redefine this pin as some other peripheral later in your code.
LiquidCrystal lcd(A2);

void setup() {
  // initialize the SPI ( Must call this before begin()! )
  lcd.initSPI();
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Hello, Sparky!");
}

void loop() {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print(millis()/1000);
  delay(100);
}

Software SPI Wiring
wiring

Software SPI Example Code

// Create an instance of the library for SOFTWARE SPI mode (define SS "latch" pin, SCLK pin, SDAT pin)
// These can be ANY of the A0 - A7 or D0 - D7 pins. 
// Just make sure you don't redefine them as some other peripheral later in your code.
LiquidCrystal lcd(D2, D3, D4);

void setup() {
  // initialize the SPI ( Must call this before begin()! )
  lcd.initSPI();
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Hello, Sparky!");
}

void loop() {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print(millis()/1000);
  delay(100);
}

wiring

:spark: Software SPI is just as fast as 9MHz Hardware SPI so don’t be afraid to use it!

:spark: Control the backlight via SPI if you have an Adafruit I2C/SPI LCD Backpack

// Create an instance of the library for SOFTWARE SPI mode (define SS "latch" pin, SCLK pin, SDAT pin)
// These can be ANY of the A0 - A7 or D0 - D7 pins. 
// Just make sure you don't redefine them as some other peripheral later in your code.
LiquidCrystal lcd(D2, D3, D4);

bool s = false;

void setup() {
  // initialize the SPI ( Must call this before begin()! )
  lcd.initSPI();
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // turn on the backlight ( Requires initSPI() to be called first )
  lcd.backlight();
  // Print a message to the LCD.
  lcd.print("Backlight Rave!");
}

void loop() {
  // Toggle the Backlight on and off, Backlight RAVE!
  if(s)
    lcd.backlight();
  else
    lcd.noBacklight();
  s = !s;
  delay(100);
}
4 Likes

VERY nice job BDub!

:sunny:

1 Like

nice job, but where can i get the hardware needed, the one i think change backlight if you dont have a backpack?

The Schematic shown here is for a standard 16x2 LCD being controlled over SPI using a 74HC595 chip (which is widely available). For controlling backlight of LCDs without backpacks, you can use a transistor to turn it on/off separately! I think there is an extra channel left on the shift register to accomplish this. What do you think @BDub ?

mohit, you can also get an intelligent I2C/SPI backpack from digole that uses the Digole library posted on another topic, obviously without all the fancy graphics features. However, it does allow for UART, II2C and SPI communications. The SPI is compatible with both hardware and software SPI. :smile:

@RickardP Here is the specific backpack I used which has the 74HC595 on-board.
http://www.adafruit.com/products/292

Check out the schematic of this to see how they did the backlight control circuit:

@mohit @RickardP A 10k ohm and 1N4401 npn transistor should do the trick for most LED backlights. If you need more than 10mA of drive current, you can decrease the 10k to a 1k for up to 100mA (assuming conservatively about 25hfe gain on the transistor and powering things from VIN. … YMMV).

@peekay123 that Digole backpack is a nice price, but it looks like they use a microcontroller to convert the SPI/I2C to LCD signals, so it’s most likely not going to work out of the box with my library.

Bdub, in regards to the Digole backpack, you are correct in that it will NOT be compatible with your library. However, I just felt it should be mentioned and an option. Keep up the great work! :monkey_face:

No problem! I’m just warning that some work will need to be done… it might not be a bad idea to support that backpack in my library as well, so if anyone figures out the differences… let me know!

FYI: The repo has been updated after dividing up the library into separate files for the Sparkulator (Web IDE) or local build environments. Some other clean up to the code was performed as well.

I’m sucha happy kid tonight with a working SPI version of 16 x 2 LCD working in under 30 minutes!

Hey @BDub :heart:

Thanks to the lovely people for giving me a people of 595 :wink:

The wires are chaotic really…time for me to work on the LCD board…

Friday nights are so ever awesome!

1 Like

Awexome!

You can add the Collector of a 2N4401 or 2N2222 NPN transistor on your K (pin 16) leg of the backlight, Emitter to GND, and Base through a 10k ohm resistor to your 74HC595 Pin 7 to utilize the LCD.backlight(); and LCD.noBacklight(); commands :wink: