Skip to content

Instantly share code, notes, and snippets.

@ramicaza
Last active October 22, 2015 15:07
Show Gist options
  • Save ramicaza/c1fafa49951767e7a31c to your computer and use it in GitHub Desktop.
Save ramicaza/c1fafa49951767e7a31c to your computer and use it in GitHub Desktop.
This is a sketch that allows the ATtiny line of micro controllers to interface with the GT511-C1/3 fingerprint sensor from Sparkfun. The advantage of this sketch is that it requires no libraries and thus works on any board that can be programmed with the Arduino IDE. The sketch initializes the connection with the sensor, turns the sensors in-bui…
#define greenLed 4 //this lights up when a registered user places their thumb
#define redLed 3 //this is on until a registerd user places their thumb
#define relay 2 //this pin goes HIGH when the registered thumb is placed
#define rxPin 0 //this goes to the TX pin of the FP sensor
#define txPin 1 //this goes to the RX pin of the FP sensor
#include <SoftwareSerial.h>;
SoftwareSerial mySerial(rxPin, txPin);
byte zero = 0;
byte highbyte = 0;
byte lowbyte = 0;
byte command = 1;
word checksum = 0;
byte highcheck = 0;
byte lowcheck = 0;
char inData[20]; // Allocate some space for the string
char correctData[] = {
};
char inChar; // Where to store the character read
int index = 0; // Index into array; where to store the character
boolean ledon = false;
int phase = 1;
boolean on = false;
void setup() {
//Serial.begin(9600);
//Serial.println("Start");
mySerial.begin(9600);
pinMode(greenLed,OUTPUT);
pinMode(redLed,OUTPUT);
pinMode(relay,OUTPUT);
inito();
}
//Here comes the part you want to put in void loop that sends the command to the device
boolean correctFinger=false;
unsigned int param;
void loop(){
if (phase == 1){
if(nbWait(200)){
//Serial.print("please place finger");
communicate(67,48);
}
}
if(mySerial.available()){
while(mySerial.available() > 0){
inChar = mySerial.read(); // Read a character
if(inChar == 0x55){//reset index if startbyte
index = 0;
}
//Serial.print("index: ");
//Serial.print(index);
//Serial.print(" ");
//Serial.print("Byte: ");
//Serial.println(inChar, HEX);
inData[index] = inChar;//save indexed character to array
if(index == 11){
//Serial.println("end of packet");
//look at response byte to check if ACK or NACK
if(inData[8] == 0x30){
//Serial.println("Acknowledge");
//different actions if acknowledged
getParams();
switch (phase){
case 1:
//phase 2 here (identification)
//Serial.println("finger pressed");
phase = 2;
communicate(73,0);
break;
case 2:
//phase 2 here (accepted identification)
phase = 3;
//Serial.print("Print ");
//Serial.print(param);
//Serial.println(" Identified, Broom broom");
if(!on){
digitalWrite(greenLed,HIGH);
digitalWrite(redLed,LOW);
digitalWrite(relay,HIGH);
on = true;
delay(1500);//time to take finger off sensor
}
else{
digitalWrite(greenLed,LOW);
digitalWrite(redLed,HIGH);
digitalWrite(relay,LOW);
on = false;
delay(1500);
}
phase = 1;//back to beginning
break;
}
}
else{
//Serial.println("Non-Acknowledge");
getParams();
switch (phase){
case 2:
//phase 3 here (denied identification)
//Serial.println("Nice try kiddo");
if(!on){
digitalWrite(redLed,LOW);
delay(100);
digitalWrite(redLed,HIGH);
}
else{
digitalWrite(redLed,HIGH);
delay(100);
digitalWrite(redLed,LOW);
}
//red led on
phase = 1;
break;
}
}
}
index++;
}
}
}
void getParams(){
param = (unsigned int)word(inData[5],inData[4]);
//Serial.print("Param: ");
//Serial.println(param);
}
void communicate(int com, int val){
int value;
switch(val){
case 48:
value = 0;
break;
case 49:
value = 1;
break;
case 50:
value = 2;
break;
case 51:
value = 3;
break;
case 52:
value = 4;
break;
case 53:
value = 5;
break;
case 54:
value = 6;
break;
case 55:
value = 7;
break;
case 56:
value = 8;
break;
case 57:
value = 9;
break;
}
switch(com){
// 0 > init > 01
case 48:
//Serial.println("Open connection");
command = 0x01;
break;
// X > close > 02
case 88:
//Serial.println("Close connection");
command = 0x02;
break;
// L > LED change > 12
case 76:
if(value == 0){
//Serial.println("LED off");
}
else {
//Serial.println("LED on");
}
command = 0x12;
break;
// T > Check total enrol count > 20
case 84:
//Serial.println("Check total enrol count");
command = 0x20;
break;
// t > check enrol id > 21
case 116:
//Serial.print("Check enrol for ID ");
//Serial.println(value);
command = 0x21;
break;
// E > enrol1 > 22
case 69:
//Serial.print("Begin enrol for ID ");
//Serial.println(value);
command = 0x22;
break;
// 1 > enrol1 > 23
case 49:
//Serial.println("Enrol scan #1");
command = 0x23;
break;
// 2 > enrol2 > 24
case 50:
//Serial.println("Enrol scan #2");
command = 0x24;
break;
// 3 > enrol3 > 25
case 51:
//Serial.println("Enrol scan #3");
command = 0x25;
break;
// F > isfingerpressed? > 26
case 70:
//Serial.println("Checking if finger pressed...");
command = 0x26;
break;
// d > delete ID > 40
case 100:
//Serial.print("Deleting scan ");
//Serial.println(value);
command = 0x40;
break;
// D > delete all > 41
case 68:
//Serial.println("Deleting all scans");
command = 0x41;
break;
// V > verify ID > 50
case 86:
//Serial.print("Verfifying ID ");
//Serial.println(value);
command = 0x50;
break;
// I > identify finger > 51
case 73:
//Serial.println("Checking for scan match...");
command = 0x51;
break;
// C > capture finger > 60
case 67:
//Serial.println("Capturing finger");
command = 0x60;
break;
// Y > ack > 30
case 89:
//Serial.println("Sending acknowledgement");
command = 0x30;
break;
// N > nack > 31
case 78:
//Serial.println("Sending Non-acknowledgement");
command = 0x31;
break;
}
//command = 0x12; //The command goes here. This is the command for the LED.
valueToWORD(value); //This value is the parameter being send to the device. 0 will turn the LED off, while 1 will turn it on.
calcChecksum(command, highbyte, lowbyte); //This function will calculate the checksum which tells the device that it received all the data
mySerial.write(0x55); //Command start code 1
mySerial.write(0xAA); //Command start code 2
mySerial.write(0x01); // This is the first byte for the device ID. It is the word 0x0001
mySerial.write(zero); // Second byte of Device ID. Notice the larger byte is first. I'm assuming this is because the datasheet says "Multi-byte item is represented as Little Endian"
mySerial.write(lowbyte); //writing the largest byte of the Parameter
mySerial.write(highbyte); //Writing the second largest byte of the Parameter
mySerial.write(zero); //The datasheet says the parameter is a DWORD, but it never seems to go over the value of a word
mySerial.write(zero); //so I'm just sending it a word of data. These are the 2 remaining bytes of the Dword
mySerial.write(command); //write the command byte
mySerial.write(zero); //again, the commands don't go over a byte, but it is sent as a word, so I'm only sending a byte
mySerial.write(lowcheck); //Writes the largest byte of the checksum
mySerial.write(highcheck); //writes the smallest byte of the checksum
}
//Now for the functions
void valueToWORD(int v){ //turns the word you put into it (the paramter in the code above) to two bytes
highbyte = highByte(v); //the high byte is the first byte in the word
lowbyte = lowByte(v); //the low byte is the last byte in the word (there are only 2 in a word)
}
void calcChecksum(byte c, byte h, byte l){
checksum = 256 + c + h + l; //adds up all the bytes sent
highcheck = highByte(checksum); //then turns this checksum which is a word into 2 bytes
lowcheck = lowByte(checksum);
}
boolean goOn = true;
void inito(){
communicate(48, 48);
while(goOn){
if(mySerial.available()){
while(mySerial.available() > 0){
inChar = mySerial.read(); // Read a character
if(inChar == 0x55){//reset index if startbyte
index = 0;
}
//Serial.print("index: ");
//Serial.print(index);
//Serial.print(" ");
//Serial.print("Byte: ");
//Serial.println(inChar, HEX);
inData[index] = inChar;//save indexed character to array
if(index == 11){
//Serial.println("end of packet");
//look at response byte to check if ACK or NACK
if(inData[8] == 0x30){
for(int u=0;u<sizeof(inData);u++){
inData[u] = 0;
}
//Serial.println("Acknowledge");
switch(phase){
case 1:
//phase 2 starts here
phase = 2;//next part of ze plan
communicate(76, 49);
break;
case 2:
phase = 3;
goOn=false;
break;
}
}
else{
//Serial.println("Non-Acknowledge");
}
}
index++;
}
}
// read from port 0, send to port 1:
}//
phase = 1;
digitalWrite(redLed,HIGH);
}
boolean first = true;
unsigned long sTime;
boolean nbWait(long waitTime){
if(first){
first = false;
sTime = millis();
}
if (millis() - sTime >= waitTime){
first = true;
return true;
}
else{
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment