Sunday, November 5, 2017

How to Build a Simple DC Electronic Load with Arduino Part 2

In this video we look at how to make a simple DC electronic load with Arduino and some simple components. In part two 2 we add some flexible measurement capabilities.







//***************************Arduino Code*************************************************
#include <Average.h> /* * This code was used for a tutorial on how to build a simple eload with Arduino * The Tutorial can be found on the ForceTronics YouTube Channel * This code is public domain and free for anybody to use at their own risk */ //Uncomment AVGMEAS to print out avg measurement data and uncomment FASTMEAS to print fast voltage or current measur #define AVGMEAS //#define FASTMEAS //uncomment to print fast current measurements, leave commented to print fast voltage measurements //#define FASTAMP //The following variables set the eload sequence const int sValues[] = {20,50,280}; //set the DAC value for each step const unsigned long sTime[] = {350,50,15}; //set the dwell time for each step const int sNum = 3; //number of steps in the sequence, this number should match the number or items in the arrays long repeat = -1; //set the number of times the sequence repeats, -1 means infinite //The following variables control the measurement rates int measInterval = 5; //in milli seconds, fast measurement rate. This should be less than or equal to measAvgInt int measAvgInt = 1000; //this is done in milliseconds and should be a multiple of the meas interval int aCount = 0; //tracks where we are in the sequence unsigned long sStart; //trcks when a sequence step starts its timer unsigned long mStart; //tracks when a new measurement interval starts unsigned long aHours = 0; //holds amp hour value const unsigned long m2Hours = 360000; //constant value for converting mil sec to hours const float lRes = 5.08; //exact value of eload resistor --> 5.08 const float rMult = 1.51; //multiplier for resistor divider network: R1 = 5.08k and R2 = 9.98k ratio is 9.98 / (9.98 + 5.08) = .663 --> 1.51 const byte aDCVolt = A2; //ADC channel for measuring input voltage const byte aDCCurrent = A4; //ADC channel for measuring voltage across resistor Average<float> voltMeas((measAvgInt/measInterval)); //create average obect to handle voltage measurement data Average<float> currMeas((measAvgInt/measInterval)); //create average object to handle current measurement data void setup() { pinMode(A0, OUTPUT); //A0 DAC pin to output analogWriteResolution(10); //default DAC resolution is 8 bit, swith it to 10 bit (max) analogReadResolution(12); //default ADC resolution is 10 bit, change to 12 bit Serial.begin(57600); analogWrite(A0, sValues[aCount]); //Set DAC value for first step sStart = mStart = millis(); //start timer for seq and measure interval } void loop() { while(repeat > 0 || repeat < 0) { //loop controls how often sequence repeats //timer for changing sequence step if(timer(sTime[aCount],sStart)) { aCount++; //go to next sequence step if(aCount >= sNum) aCount = 0; //if at end go back to beginning analogWrite(A0, sValues[aCount]); //Set DAC value for step sStart = millis(); //reset timer } if(timer(measInterval,mStart)) { voltMeas.push(inputVolt(aDC2Volt(analogRead(aDCVolt)))); //push value into average array currMeas.push(inputCurrent(aDC2Volt(analogRead(aDCCurrent)))); //push value into average array //print input voltage value and current values #ifdef FASTMEAS #ifdef FASTAMP Serial.println(currMeas.get((currMeas.getCount() - 1))*1000); //serial print out of fast current measurements #else Serial.println(voltMeas.get((voltMeas.getCount() - 1))); //serial print out of fast voltage measurements #endif #endif mStart = millis(); //reset timer } //print out average, max / min, and amp hour measurements if(voltMeas.getCount() == (measAvgInt/measInterval)) { #ifdef AVGMEAS Serial.print("Average voltage: "); Serial.print(voltMeas.mean()); Serial.println(" V"); //get and print average voltage value float mA = currMeas.mean()*1000; //get average current value in mA Serial.print("Average current: "); Serial.print(mA); Serial.println(" mA"); //print current value Serial.print("Max voltage: "); Serial.print(voltMeas.maximum()); Serial.println(" V"); //print max and min voltage Serial.print("Min voltage: "); Serial.print(voltMeas.minimum()); Serial.println(" V"); Serial.print("Max current: "); Serial.print(currMeas.maximum()*1000); Serial.println(" mA"); //print max and min current Serial.print("Min current: "); Serial.print(currMeas.minimum()*1000); Serial.println(" mA"); float aH = ampHoursCal(measAvgInt,mA); //calculate how much amp hours of current was consumed since start if(aH < 1000) { Serial.print("Amp hours of power source: "); Serial.print(aH); Serial.println(" uAh"); } //print current in uA else { Serial.print("Amp hours of power source: "); Serial.print(aH/1000); Serial.println(" mAh"); } //print current in mA #endif voltMeas.clear(); //clear voltage measurement array currMeas.clear(); //clear current measurement array } if(repeat > 0) repeat--; //increment repeat if not infinite loop } } //timer function that runs in mill second steps. //Inputs are timer interval and timer start time bool timer(unsigned long tInterval, unsigned long tStart) { unsigned long now = millis(); //get timer value if ((now - tStart) > tInterval ) return true; //check if interval is up return false; //interval is not up } //converts raw ADC reading to voltage value based on 3.3V reference //input is 12 bit ADC value float aDC2Volt(int aDC) { return (((float)aDC/4095)*3.3); } //function converts voltage value to input voltage value based off resistor voltage divider constant //input is measured voltage float inputVolt(float aVolt) { return (rMult*aVolt); } //converts voltage measurement at load resistor to current measurement based on load resistor value //Input is measured voltage float inputCurrent(float rVolt) { return (rVolt/lRes); } //This functions calculates amp hours //amp hour = amp hour value + (amps * (mil sec / 360k) //input: measInt is measurement interval in milli sec and aVal is the measured current value in mA float ampHoursCal(int measInt, float aVal) { aHours = aHours + (aVal * ((double)measInt/m2Hours)*1000); //this converts currect measurement to mA return aHours; }



Wednesday, October 11, 2017

How to Build a Simple DC Electronic Load with Arduino Part 1

In this video we look at how to make a simple DC electronic load with Arduino and some simple components.





//****************Arduino code from video************
/*
 * This code was used for a tutorial on how to build a simple eload with Arduino
 * The Tutorial can be found on the ForceTronics YouTube Channel
 * This code is public domain and free for anybody to use at their own risk
 */
void setup() {
  pinMode(A0, OUTPUT); //A0 DAC pin to output
  analogWriteResolution(10); //default DAC resolution is 8 bit, swith it to 10 bit (max)
}

void loop() {
  //create pulsed current profile
  analogWrite(A0, 16); //Set DAC to approximately 10mV --> current 10mV / 5ohm = 2 mA
  delay(500);
  analogWrite(A0,310); //Set DAC to 1V --> current 1V / 5ohm = 200 mA
  delay(50);

}

Monday, September 11, 2017

Two Methods for Soldering Surface Mount Components by Hand

When you are prototyping, repairing, or hacking PCB boards you will most definitely find yourself in situations where you need to solder surface mount components / devices (SMD) by hand. In this video we look at two easy methods to solder SMD by hand with a basic solder station.


Wednesday, September 6, 2017

Easy Way to Design a PCB for the Ublox Neo-7m GNSS / GPS Module Part 2

In this tutorial we look at how easy it is to integrate the popular Neo-7m GNSS / GPS module into your design without having to be an RF / microwave expert. In part 2 we build up our board and take it for a test drive.




Link to Eagle files on GitHub: https://github.com/ForceTronics/Neo-7m_Eagle_Design/tree/master

Friday, August 25, 2017

Easy Way to Design a PCB for the Ublox Neo-7m GNSS / GPS Module

In this tutorial we look at how easy it is to integrate the popular Neo-7m GNSS / GPS module into your design without having to be an RF / microwave expert.


Link to Eagle files on GitHub: https://github.com/ForceTronics/Neo-7m_Eagle_Design/tree/master

Thursday, August 10, 2017

Getting Started with Arduino and the ThingSpeak Cloud

In this video we look at how to use Arduino with the ThingSpeak IoT, cloud, and analytics platform. In the video we look at two use cases of ThingSpeak. For the first use case we log wireless sensor data to the cloud and perform post processing on the data using MATLAB. In the second use case we monitor a room with a wireless motion detector setup and have ThingSpeak send out a Tweet if any movement is detected in the room.



//*****************Arduino code from video**********************************
/*
This sketch was created for a video tutorial on the ForceTronics YouTube that shows how to use Arduino with the ThingSpeak cloud
This code is public domain and free for anybody to use or modify at their own risk

Note this code was leveraged from:
 Arduino --> ThingSpeak Channel via MKR1000 Wi-Fi
 Created: May 7, 2016 by Hans Scharler (http://www.nothans.com)
*/
   
#include <SPI.h>
#include <WiFi101.h> //This sketch should work with any Arduino or shield that can use the WiFi101 library

char ssid[] = "YourNetwork"; //  your network SSID (name)
char pass[] = "YourPassword"; // your network password

int status = WL_IDLE_STATUS;

// Initialize the Wifi clients
WiFiClient tClient; //this one is used to post temperature data
WiFiClient mClient; //this one is used to post motion detection data

// ThingSpeak Settings
char server[] = "api.thingspeak.com";
String writeAPIKey = "YourWriteKey";

//Timing variables for tracking temperature posting
unsigned long tempLastConnectionTime = 0; // track the last connection time
const unsigned long tempPostingInterval = 295000L; // post temp ADC value just under every 5 min

//Timing and logic variables for tracking temperature posting
unsigned long mLastConnectionTime = 0; // track the last connection time
const unsigned long mPostingInterval = 30000L; //checks to see if motion was detected every 30 sec
bool mReset = true; //tracks if motion detection has been reset

void setup() {
  pinMode(2,INPUT); //digital pin used to read output of motion detector
  pinMode(6,OUTPUT); //LED pin used to know when data is sent to cloud
  
  // attempt to connect to Wifi network
  while ( status != WL_CONNECTED) {
    // Connect to WPA/WPA2 Wi-Fi network
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection
    delay(10000);
  }
}

void loop() {
  //timer to know when it is time to post temp data to cloud
  if (millis() - tempLastConnectionTime > tempPostingInterval) {
    digitalWrite(6,HIGH); //Use the LED to see if Arduino gets heldup posting data to the cloud
    tempHttpRequest(); //function that formats data strings and posts data to ThingSpeak cloud
    digitalWrite(6,LOW);
  }

  if (millis() - mLastConnectionTime > mPostingInterval) {
    digitalWrite(6,HIGH); //Use the LED to see if Arduino gets heldup posting data to the cloud
    if(digitalRead(2) && mReset) { //if motion was detected post it
      mReset = false; //motion detected so reset variable
      mHttpRequest(); //if true motion was detected so post to cloud
    }
    else mReset = true; //reset logic tracker
    
    digitalWrite(6,LOW);
  }

}

//Posts temp data to thingspeak cloud
void tempHttpRequest() {
  // read analog pin 0 with temp sensor connected
  int sensorValue = analogRead(0);

  // create data string to send to ThingSpeak
 String data = String("field1=" + String(sensorValue, DEC)); 

  // POST data to ThingSpeak
  if (tClient.connect(server, 80)) {
    tClient.println("POST /update HTTP/1.1");
    tClient.println("Host: api.thingspeak.com");
    tClient.println("Connection: close");
    tClient.println("User-Agent: ArduinoWiFi/1.1");
    tClient.println("X-THINGSPEAKAPIKEY: "+writeAPIKey);
    tClient.println("Content-Type: application/x-www-form-urlencoded");
    tClient.print("Content-Length: ");
    tClient.print(data.length());
    tClient.print("\n\n");
    tClient.print(data);
     // close any connection before sending a new request
    tClient.stop();
    // note the last connection time
    tempLastConnectionTime = millis();
  }
}

//posts motion detection data to thingspeak cloud
void mHttpRequest() {

  // create data string to send to ThingSpeak, we always send a one here to indicate motion detected
 String data = String("field2=" + String(1,DEC)); 

  // POST data to ThingSpeak
  if (mClient.connect(server, 80)) {
    mClient.println("POST /update HTTP/1.1");
    mClient.println("Host: api.thingspeak.com");
    mClient.println("Connection: close");
    mClient.println("User-Agent: ArduinoWiFi/1.1");
    mClient.println("X-THINGSPEAKAPIKEY: "+writeAPIKey);
    mClient.println("Content-Type: application/x-www-form-urlencoded");
    mClient.print("Content-Length: ");
    mClient.print(data.length());
    mClient.print("\n\n");
    mClient.print(data);
     // close any connection before sending a new request
    mClient.stop();
    // note the last connection time
    mLastConnectionTime = millis();
  }
}

%*************************MATLAB code from video***************************
% Channel ID to read raw ADC temp data from
readChannelID = chanID;
% Temperature Field ID
TemperatureFieldID = 1;

% Channel Read API Key 
readAPIKey = 'YourReadKey';

% Channel ID to write temp data to:
writeChannelID = [chanID];
% API key for write channel:
writeAPIKey = 'YourWriteKey';

%Read raw ADC temp data
aDC = thingSpeakRead(readChannelID, 'Fields', TemperatureFieldID, 'ReadKey', readAPIKey);

%Convert raw 10 bit ADC data to voltage
volt = aDC*(3.3/1023);

%Using analog temp sensor TMP36
%calculate temp in C, .75 volts is 25 C. 10mV per degree
 if volt < .75 
     temp = 25 - ((.75-volt)/.01); %if below 25 C
 elseif volt == .75 
         temp = 25;
 else 
     temp = 25 + ((volt -.75)/.01);  %if above 25
 end

% Convert to Fahrenheit
tempF = (9/5*temp) + 32;

%This writes temp value to below console for debugging
display(tempF);

%write temp F value to channel
thingSpeakWrite(writeChannelID, 'Fields',1,'Values',tempF, 'Writekey', writeAPIKey);

Saturday, June 10, 2017

Using Compiler Directives with Arduino

In this video we look at what are compiler directives and how they can come in handy.



//***********************Arduino code from the video***********
/*
 * This code was made for a video tutorial on ForceTronics YouTube Channel called "Using Compiler Directives with Arduino"
 * This code free to be used or modified by anybody at your own risk
 * 
 */


//#define SERIAL_PRINT 1
#define ADC_PIN (uint8_t)A0

 #ifdef SERIAL_PRINT
  #define _SERIAL_BEGIN(x) Serial.begin(x);
  #define _SERIAL_PRINT(x) Serial.print(x);
  #define _SERIAL_PRINTLN(x) Serial.println(x);
 #else
  #define _SERIAL_BEGIN(x)
  #define _SERIAL_PRINT(x)
  #define _SERIAL_PRINTLN(x)
 #endif


void setup() {
  
  _SERIAL_BEGIN(57600); //start serial comm if enabled

  //This will only print if SERIAL_PRINT is 1
  _SERIAL_PRINTLN("We are going to take an ADC reading every 2 sec...");

}

void loop() {
  _SERIAL_PRINT("The latest ADC measurement is ");
  _SERIAL_PRINTLN(analogRead(ADC_PIN));
  delay(2000); 

}