270o Final Project

From CSclasswiki
Jump to: navigation, search

Lizette's Project Page

Introduction

Welcome to the Wiki Page of my Final Project for CSC270: Digital Circuits Systems. The purpose of this project is to create three configurations using various Elegoo Gadgets including a Mega2560 Arduino, Raspberry Pi and a 64-LED matrix. These configurations will each be able to execute a task as detailed in the outline.

Plan

Outline

The objectives explained in the Introduction will be achieved in 3 parts:

  1. Connect a 64-LED matrix display to a Mega2560 Arduino and program it to display my name, "L I Z E T T E".
  1. Demonstrate a master - worker model by establishing an i2c link between a Raspberry Pi and a Mega2560 Arduino. The Raspberry Pi will send a word to the Arduino. The Arduino will implement a Ceaser Cipher on the word, then send it back to the Raspberry Pi.
  1. Retrieve Seattle, Washington's current weather from weather.com using a Raspberry Pi. The Raspberry Pi will send it to the Arduino, which will display the data on the 64-LED matrix display.

I will be programming the Raspberry Pi and Arduino in C.

Materials

  1. Mega2560 Arduino
  2. Raspberry Pi 3
  3. 64-LED Matrix
  4. Breadboard
  5. Wires
  6. Resistors

Resources

Google Chrome LED Byte Generator: This is used to generate the byte array matrix for each letter that is displayed on the 64-LED matrix in Part 1.

Part 1: Connect and Program 64-LED matrix to Arduino

Summary

I followed the Arduino 8×8 LED Matrix Tutorial [1] to connect the 64-LED matrix to my Arduino. The five pins of the 64-LED matrix were wired to the Arduino where DIN, CS, and clock where wired to pins 12, 11, and 10, respectively and the VCC and GND pins were wired to GND and 5V of the Arduino.

Once everything was wired, I wrote a sketch that displays the letters of my name, LIZETTE, one at a time. The sketch declares the pins wired between the LED matrix and the Arduino and the hexadecimal bytes. The hexadecimal bytes were generated using a Google Chrome extension called LED Byte Generator [2]. A function, printByte(), prints a byte by lighting up the rows of the matrix according to its hexadecimal. All the bytes gets called by printByte() in the setup() as demonstrated in the video below. This sketch used a library called LedControl [3].

Images & Videos



Sketch

/*
 * Lizette Carpenter 
 * Part 64-LED Matrix and Arduino Configuration 
 * The 64-LED Matrix will display my name: "L I Z E T T E"
 * 
 * References: 
 * LED Control library: 
 * Google Chrome app: LED Byte Generator 
 */

//Include the following library 
#include <LedControl.h>

//Declare the pins of the Arduino following how it is connected to the pins of the LED Matrix
int DIN = 12;
int CS = 11; 
int CLK = 10; 

//Declare the byte arrays which represent the letters that will display on the LED Matrix 
byte l[8] = {0x0,0x40,0x40,0x40,0x40,0x40,0x7e,0x0};
byte i[8] = {0x0,0x8,0x8,0x8,0x8,0x8,0x8,0x0};
byte z[8] = {0x0,0x7e,0x4,0x8,0x10,0x20,0x7e,0x0};
byte e[8] = {0x0,0x7c,0x40,0x7c,0x40,0x40,0x7c,0x0};
byte t[8] = {0x0,0x7f,0x8,0x8,0x8,0x8,0x8,0x0};
byte T[8] = {0x0,0x0,0xfe,0x10,0x10,0x10,0x10,0x10};


//Initialize the LED control library 
LedControl lc=LedControl(DIN, CLK, CS, 0);

void setup() {
  lc.shutdown(0, false); 
  lc.setIntensity(0,15); 
  lc.clearDisplay(0); 
  
}

void loop() {
  printByte(l); 
  delay(1000);

  printByte(i); 
  delay(1000);

  printByte(z); 
  delay(1000);

  printByte(e); 
  delay(1000);

  printByte(t); 
  delay(1000);

  printByte(T); 
  delay(1000);

  printByte(e); 
  delay(1000);
}

//Loops through each byte array and displays its corresponding letter to the LED Matrix. 
void printByte(byte character []) {
  int i = 0; 
  for (i = 0; i<8; i++) {
    lc.setRow(0, i, character[i]); 
  } 
}

Part 2: i2c link between Raspberry Pi to the Arduino

Summary

Working on this part of the project was very interesting and fun since I was able to practice what I learned in class such as i2c communications and also learn on my own how to code in C. I first followed the I2C with Arduino and Raspberry Pi - Two Method. I first assembled the hardware by making the Raspberry Pi the Master since it uses 3.3 V logic. Arduino would not be able to be the Master because of its higher voltage logic and the way the varying voltage logic of these devices would interact. I connected the GND GPIO 20 of the RPi to a GND of the Arduino. I then connected the SDA, (GPIO 3) and the SCL, (GPIO 5), to Arduino's SDA (Pin 21) and SCL (Pin 20).

Once the hardware was all setup, I proceeded to work on the programs for the Raspberry RPi and Arduino. I decided to make the devices, while connected via I2C, to communicating with each other in an unique way. In the RPI program, I prompted the user to input a message to be encrypted, using a ceaser shift, by the Raspberry Pi. Then the RPi writes or sends the encrypted message to the Arduino where it reads it and decrypts the message. The Arduino sends the decrypted message back to the RPi, where the RPi prints the final results.

There is a caveat to my program. I originally wanted the user to be able to decide the shift value of the ceaser shift; however, I struggled to write data blocks from the Raspberry Pi to the Arduino. Thus, I only wanted one form of input from the user and I created a constant ceaser shift valuem, 17.

Images & Videos

Running master i2c
Hardware for i2c communication between RPi and Arduino

Sketch

Raspberry Pi Code, Python

'''master_i2c.py                                                                                                                                                                                                                             
                                                                                                                                                                                                                                             
Lizette Carpenter                                                                                                                                                                                                                            
Final Project Part 2                                                                                                                                                                                                                         
CSC270                                                                                                                                                                                                                                       
                                                                                                                                                                                                                                             
This program helps demonstrate an i2c communication between a Raspberry Pi and Arduino                                                                                                                                                       
by conducting a ceaser shift encryption and decryption on a user input.                                                                                                                                                                      
Once this program has encrypted the message, it writes the encryption to the Arduino.                                                                                                                                                        
It then waits for the Arduino to send back the message, decrypted. Once it receives                                                                                                                                                          
the decrypted message, this program converts the ASCII values into characters and                                                                                                                                                            
prints the results.                                                                                                                                                                                                                          
'''

from smbus2 import SMBus
import smbus
import time

def printResults(results):
        print("Arduino wanted to join the fun. They decrypted your encryption:")
        for i in range(len(results)):
                print(results[i]),

def StringToBytes(val):
        results = []
        addr = 0x8
        for c in val:
                data = ord(c)
                with SMBus(1) as bus:
                        bus.write_byte(addr, data)
                        time.sleep(0.1)
                        decryption = chr(bus.read_byte(addr))
                        results.append(str(decryption))
        return printResults(results)

def decrypt(cipher):
    byteValue = StringToBytes(cipher)

def encrypt(message, key):
    cipher = ""
    for i in range(len( message)):
        char = message[i]
        cipher += chr((ord(char) + int(key) - 97) % 26 + 97)
    print("Your encryption is: " + cipher)
    return decrypt(cipher)

def main():
    key = 17
    message = raw_input("Enter a message in lowercase: ")
    print("Your shift number is 17")
    encrypt(message, key)

main()

Arduino Code, C

 
#include <Wire.h>

int result = 0; 
byte cipher = 0;
void setup() {
  Wire.begin(0x8);
  Wire.onReceive(receiveData);
  Wire.onRequest(sendData);
}
void loop() { 
  
}

void receiveData(int byteCount) {
  while (Wire.available()) {
    cipher = Wire.read();
    result = (cipher + 9 - 97) % 26 + 97;
  }
}
     
void sendData() {
  Wire.write(result);
}