Tuesday, August 11, 2015

Building Your Own AVR / Arduino Internet of Things (IoT) Development Board Part 5

Welcome to the final installment of building your own AVR / Arduino compatible internet of things (IoT) development board. In part 5 we open up our new PCBs and build them up. We then do some testing to make sure everything is working correctly. Spoiler alert, this video has a happy ending with a working Arduino compatible board with Bluetooth 4.0 built-in. Below you will find a link to the Eagle files and the Arduino code for the test sketch.



Link to download Eagle files including: library file, project files, and Gerber files:
https://dl.dropboxusercontent.com/u/26591541/AVR_IoT_Board_Eagle_Files_8_8_15.zip


*******************Arduino Test Sketch************************************
//This sketch is to test a DIY Arduino compatiable board with Bluetooth 4.0 on it
//the board is targeted IoT applications. Details can be found of the ForceTronics 
//YouTube channel. This code is open for anybody to use and modify

int wValue = 1; //variable to hold write value (high or low)

void setup() {
   Serial.begin(115200); //start serial
}

void loop() {
  delay(1000);
  setPinMode(OUTPUT, INPUT); //set D2 to D7 as outputs, and D8 to D13 to inputs
  setDigWrite(false, wValue); //set group of pins to write and write high or low
  Serial.print("D2 thru D7 writing "); 
  Serial.println(wValue);
  Serial.println("D8 thru D13 reading the following values....");
  printDigPins(true); //print what dig pins read
  delay(1000);
  setPinMode(INPUT, OUTPUT); //set D2 to D7 as intputs, and D8 to D13 to outputs
  setDigWrite(true, wValue); //set group of pins to write and write high or low
  Serial.print("D8 thru D13 writing "); 
  Serial.println(wValue);
  Serial.println("D2 thru D7 reading the following values....");
  printDigPins(false); //print what dig pins read
  
  delay(1000);
  adcReads(); //read each ADC pin and print result
  
  if(wValue) wValue = 0; //toggle digital write value
  else wValue = 1;
  delay(1000);
}

//function sets D2 thru D7 to input / output and D8 thru D13 to input / output
void setPinMode(int smallMode, int bigMode) {
  for(int i=0;i<6;i++) {
    pinMode((i+2), smallMode);
    pinMode((i+8), bigMode);
  }  
}

//Writes high or low to group of digital pins
void setDigWrite(bool big, int state) {
  if(big) {
    for(int i=0;i<6;i++) {
      digitalWrite((i+8), state);
    }
  }
  else {
    for(int i=0;i<6;i++) {
      digitalWrite((i+2), state);
    }
  }
}

//does digital read on group of digital pins and prints results
void printDigPins(bool big) {
  if(big) {
    for(int i=0;i<6;i++) {
      Serial.print("Value at pin D");
      Serial.print((i+8));
      Serial.print(" --> ");
      Serial.println(digitalRead((i+8)));
    }
  }
  else {
    for(int i=0;i<6;i++) {
      Serial.print("Value at pin D");
      Serial.print((i+2));
      Serial.print(" --> ");
      Serial.println(digitalRead((i+2)));
    }
  }
}


//Reads each ADC pin and prints result
void adcReads() {
  for(int i=0; i<6; i++) {
    Serial.print("ADC value at Pin A");
    Serial.print(i);
    Serial.print(" --> ");
    Serial.println(analogRead(i));
  }
}

Sunday, August 2, 2015

Building a Not Gate from a Transistor

In this video we take a brief look at how a transistor works and how to use it as a not gate or an digital logic inverter. We look at an example with Arduino using both an NPN and a PNP bipolar junction transistor as a not gate



//***********Arduino Code ******************************************
//This example code was used on the Forcetronics YouTube Channel to demonstrate how to 
//make an inverter or not gate using an NPN and PNP transistor

const int NPN = 2; //create variable for NPN base 
const int PNP = 3; //create variable for PNP base
const int NPNRead = 4; //create variable to read NPN
const int PNPRead = 5; //create variable to read PNP

void setup() {
  pinMode(NPN, OUTPUT);   // set pin to output
  pinMode(PNP, OUTPUT);   // set pin to output
  pinMode(NPNRead, INPUT);   // set pin to input
  pinMode(PNPRead, INPUT);   // set pin to input
  Serial.begin(9600); //start serial comm
}

void loop() {
  digitalWrite(NPN, LOW);   // set NPN base to low
  digitalWrite(PNP, LOW);   // set PNP base to low
  Serial.println("Base of NPN and PNP is set to Low");
  Serial.print("NPN reads ");
  Serial.println(digitalRead(NPNRead)); //read and print value at NPN digital pin
  Serial.print("PNP reads ");
  Serial.println(digitalRead(PNPRead)); //read and print value at PNP digital pin
  Serial.println();
  delay(1000);
  digitalWrite(NPN, HIGH);   // set NPN base to high
  digitalWrite(PNP, HIGH);   // set PNP base to high
  Serial.println("Base of NPN and PNP is set to High");
  Serial.print("NPN reads ");
  Serial.println(digitalRead(NPNRead)); //read and print value at NPN digital pin
   Serial.print("PNP reads ");
  Serial.println(digitalRead(PNPRead)); //read and print value at PNP digital pin
  Serial.println();
  delay(1000);
}