Project 9a: 16×2 LCD Stock Ticker

What you will need:

  • Raspberry Pi
  • Breadboard
  • 16 x 2 LCD Screen
  • 10kΩ Potentiometer
  • 8 Female to Male Jumper Wires
  • 7 Male to Male Jumper Wires

Instructions:

Once my son and I were able to get our 16 x 2 LCD Screen operating with the Raspberry Pi, we decided to create some simple projects that utilize it. Our first variation, uses the identical breadboard layout from Project 9 and creates a simple stock ticker by using the Yahoo! Finance API.

This project builds upon Project 9 and requires the Adafruit Raspberry Pi Python Code Library. Be sure to follow the instructions in our Previous Project outlining the installation procedures before you continue.

Once you have your breadboard populated and the Adafruit Library cloned, enter the Python script below using your favorite editor:

#!/usr/bin/env python
#
#  stock_ticker_LCD.py
#
# A short program using the Yahoo Finance API and
# Adafruit's 'Adafruit_CharLCD' library to monitor
# stock values and to
# update a 16x2 LCD display in 4 bit mode
#
#
# LCD Screen 16x2 Pins
# --------------------
# Pin 1  to GND
# Pin 2  to 5v
# Pin 3  to variable resistor then to GND 'Contrast Adjustment'
# Pin 4  to GPIO 25 for 'Command or Data Mode Switching'
# Pin 5  to GND for 'Read/Write Mode'
# Pin 6  to GPIO 24 for 'Enable Pin'
# Pin 7  to Not Connected
# Pin 8  to Not Connected
# Pin 9  to Not Connected
# Pin 10 to Not Connected
# Pin 11 to GPIO 23 for 'Data 4'
# Pin 12 to GPIO 17 for 'Data 5'
# Pin 13 to GPIO 21 for 'Data 6'
# Pin 14 to GPIO 22 for 'Data 7'
# Pin 15 to 5v for 'Backlight Anode' - Could use variable resistor as a 'Brightness Control'
# Pin 16 to GND for 'LED Backlight Cathode'
#
#  Copyright 2015  Ken Powers
# 

# Import the required libraries
from Adafruit_CharLCD import Adafruit_CharLCD
import urllib2
import json
import time
import RPi.GPIO as GPIO

# Set GPIO pins to Broadcom numbering system
GPIO.setmode(GPIO.BCM)

lcd = Adafruit_CharLCD()
RUNNING = True

# Create a list of stocks to check
stocks = ("^IXIC","YHOO","AAPL","WPZ","ETE","OKE","MWE")

try:
	while RUNNING:
		# Use JSON to obtain stock information
		# using an API call to Yahoo Finance

		for i in range(len(stocks)):
			f = urllib2.urlopen('http://download.finance.yahoo.com/d/quotes.csv?s=' + stocks[i] + '&f=sghn')
			json_string = f.read()

			# Create a list from the returned information by splitting at the comma character
			stockquotes = json_string.split(",")

			# Strip the quotes from the stock name in the first item
			stockquotes[0] = stockquotes[0].strip('"')
			stockdiff = float(stockquotes[2]) - float(stockquotes[1])

			# Print the stock information to the screen
			print(stockquotes[0])
			print("Low: %s" % stockquotes[1])
			print("High: %s" % stockquotes[2])
			print(stockdiff)
			print("\n")

			# Send information to 16x2 LCD screen Using Adafruit_CharLCD
			lcd.clear()
			lcd.home()
			# Set number of columns and rows of LCD display
			lcd.begin(16,2)
			# Set cursor position and scroll info from right to left
			lcd.setCursor(17,0)
			if stockdiff > 0:
				updown = '   ^'
			else:
				updown = '   v'
			lcd.message(stockquotes[0] + updown + str(abs(stockdiff)))
			lcd.setCursor(17,1)
			ticker = ('L: ' + stockquotes[1] + '  H: ' + stockquotes[2])
			lcd.message(ticker)
			for x in range(0, len(ticker)):
				lcd.DisplayLeft()
				time.sleep(.25) 

			time.sleep(.1)
			f.close()

except KeyboardInterrupt:
	RUNNING = False
	print "\nQuitting"

# Clear LCD screen upon exit
# Don't forget to clean up after so we
# can use the GPIO next time
finally:
	lcd.clear()
	GPIO.cleanup()

Code Analysis:

As in previous projects, our Raspberry Pi’s Python script begins by letting the interpreter know which language we are using as well as providing some comments that indicate the purpose of our program. We also take some effort to make some LCD screen GPIO pin connection notes for added clarity.

#!/usr/bin/env python
#
#  stock_ticker_LCD.py
#
# A short program using the Yahoo Finance API and
# Adafruit's 'Adafruit_CharLCD' library to monitor
# stock values and to
# update a 16x2 LCD display in 4 bit mode
#
#
# LCD Screen 16x2 Pins
# --------------------
# Pin 1  to GND
# Pin 2  to 5v
# Pin 3  to variable resistor then to GND 'Contrast Adjustment'
# Pin 4  to GPIO 25 for 'Command or Data Mode Switching'
# Pin 5  to GND for 'Read/Write Mode'
# Pin 6  to GPIO 24 for 'Enable Pin'
# Pin 7  to Not Connected
# Pin 8  to Not Connected
# Pin 9  to Not Connected
# Pin 10 to Not Connected
# Pin 11 to GPIO 23 for 'Data 4'
# Pin 12 to GPIO 17 for 'Data 5'
# Pin 13 to GPIO 21 for 'Data 6'
# Pin 14 to GPIO 22 for 'Data 7'
# Pin 15 to 5v for 'Backlight Anode' - Could use variable resistor as a 'Brightness Control'
# Pin 16 to GND for 'LED Backlight Cathode'
#
#  Copyright 2015  Ken Powers
#

Next, we import the required modules and libraries necessary for our script to operate properly. We also create our constants, instantiate an LCD using the Adafruit library, and create a list of the stock symbols we wish to check with our ticker. Feel free to modify this list to reflect your own stock interests.

# Import the required libraries
from Adafruit_CharLCD import Adafruit_CharLCD
import urllib2
import json
import time
import RPi.GPIO as GPIO

# Set GPIO pins to Broadcom numbering system
GPIO.setmode(GPIO.BCM)

lcd = Adafruit_CharLCD()
RUNNING = True

# Create a list of stocks to check
stocks = ("^IXIC","YHOO","AAPL","WPZ","ETE","OKE","MWE")

Our main loop simply builds a URL for each of the stock symbols in our list using the Yahoo! Finance API. It then parses the returned information, removes the quotation marks, and prints the stock information to our main screen.

Next, the loop clears our LCD screen, sets the cursor position, displays the stock information, scrolls the data to the left, and continues until Ctrl + C is pressed.

try:
	while RUNNING:
		# Use JSON to obtain stock information
		# using an API call to Yahoo Finance

		for i in range(len(stocks)):
			f = urllib2.urlopen('http://download.finance.yahoo.com/d/quotes.csv?s=' + stocks[i] + '&f=sghn')
			json_string = f.read()

			# Create a list from the returned information by splitting at the comma character
			stockquotes = json_string.split(",")

			# Strip the quotes from the stock name in the first item
			stockquotes[0] = stockquotes[0].strip('"')
			stockdiff = float(stockquotes[2]) - float(stockquotes[1])

			# Print the stock information to the screen
			print(stockquotes[0])
			print("Low: %s" % stockquotes[1])
			print("High: %s" % stockquotes[2])
			print(stockdiff)
			print("\n")

			# Send information to 16x2 LCD screen Using Adafruit_CharLCD
			lcd.clear()
			lcd.home()
			# Set number of columns and rows of LCD display
			lcd.begin(16,2)
			# Set cursor position and scroll info from right to left
			lcd.setCursor(17,0)
			if stockdiff > 0:
				updown = '   ^'
			else:
				updown = '   v'
			lcd.message(stockquotes[0] + updown + str(abs(stockdiff)))
			lcd.setCursor(17,1)
			ticker = ('L: ' + stockquotes[1] + '  H: ' + stockquotes[2])
			lcd.message(ticker)
			for x in range(0, len(ticker)):
				lcd.DisplayLeft()
				time.sleep(.25) 

			time.sleep(.1)
			f.close()

except KeyboardInterrupt:
	RUNNING = False
	print "\nQuitting"

# Clear LCD screen upon exit
# Don't forget to clean up after so we
# can use the GPIO next time
finally:
	lcd.clear()
	GPIO.cleanup()

This was a fun project because we got to see our LCD screen interact with real data from the Internet and the American stock market. Have fun fiddling with which stock data is being read as well as the way the LCD screen displays the information. Above all, however, have fun!