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));
  }
}