Showing posts with label battery powered. Show all posts
Showing posts with label battery powered. Show all posts

Thursday, December 11, 2014

Reducing Arduino’s Power Consumption Part 3

Welcome to part 3 of reducing Arduino's power consumption, a must watch series for anybody building a battery powered project with Arduino. In part 3 we will look at how to use the Watch Dog Timer like an alarm clock to wake Arduino up from sleep mode. We we also look at some additional techniques to save power.


*****************************************Arduino Code*************************************************
/*
Example program for using sleep modes and watch dog timer in Arduino. This example code was used in a sleep mode tutorial video on the ForceTronics YouTube Channel.
This code is open for anybody to use at their own risk*/
     
/*WDT BYTE variables for setting timer value
     WDTO_15MS
WDTO_30MS
WDTO_60MS
WDTO_120MS
WDTO_250MS
WDTO_500MS
WDTO_1S
WDTO_2S
WDTO_4S
WDTO_8S */
     
#include <avr/sleep.h>
//We use part of the WDT library, but have to use registers as well since library does not support interrupt mode for WDT
#include <avr/wdt.h> 

int led = 13; //variable for pin that the LED is on
int tog = 1; //variable that toggles between traditional delay() function and WDT sleep delay function

void setup() {
  wdt_disable(); //Datasheet recommends disabling WDT right away in case of low probabibliy event
   pinMode(led, OUTPUT); //set up the LED pin to output
}

void loop() {
  
  if(tog) { //use traditional delay function
    digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(1000);               // wait 
    digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
    delay(1000);       // wait 
    tog = 0; //toggle variable
  }
  else { //after blinking LED setup interrupt and then go to sleep. Note that sleep will only happen once sinc
    digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(1000);   // turn the LED on (HIGH is the voltage level)//  
    digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
    delayWDT(WDTO_1S);   // Use WDT sleep delay function, argument is byte variable from WDT Library
    //delayWDT(0x06);      //Use WDT sleep delay function, argument is byte value that sets timer to 1 second
    tog = 1; //toggle variable
  }
   
}

//This function serves as a power saving delay function. The argument is a Byte type variable that is used to set the delay time
//The function sets up sleep mode in power down state. The function then sets up the WDT timer in interrupt mode and sets it.
//It then puts the Arduino to sleep for the set time. Upon wake up the WDT and sleep mode are shut off
void delayWDT(byte timer) {
  sleep_enable(); //enable the sleep capability
  set_sleep_mode(SLEEP_MODE_PWR_DOWN); //set the type of sleep mode. Default is Idle
  ADCSRA &= ~(1<<ADEN); //Turn off ADC before going to sleep (set ADEN bit to 0)
  WDTCSR |= 0b00011000;    //Set the WDE bit and then clear it when set the prescaler, WDCE bit must be set if changing WDE bit   
  WDTCSR =  0b01000000 | timer; //Or timer prescaler byte value with interrupt selectrion bit set
 // WDTCSR = 0b01000110; //This sets the WDT to 1 second
  wdt_reset(); //Reset the WDT 
  sleep_cpu(); //enter sleep mode. Next code that will be executed is the ISR when interrupt wakes Arduino from sleep
  sleep_disable(); //disable sleep mode
  ADCSRA |= (1<<ADEN); //Turn the ADC back on
}

//This is the interrupt service routine for the WDT. It is called when the WDT times out. 
//This ISR must be in your Arduino sketch or else the WDT will not work correctly
ISR (WDT_vect) 
{
  wdt_disable();
   MCUSR = 0; //Clear WDT flag since it is disabled, this is optional

}  // end of WDT_vect

Sunday, November 30, 2014

Reducing Arduino’s Power Consumption Part 2

In Part two, of this three part series, we will look at how to use the Power Reduction Register (PRR) to reduce power consumption by turning off parts of Arduino that are not being used. This series is for anybody using Arduino for a battery powered project where maximizing battery life is critical.



*************************Arduino Code***********************************
/* This Arduino Sketch is part of a tutorial on the ForceTronics YouTube Channel and demonstrates how to use the 
Power Reduction Registers (PRR). It is free and open for anybody to use at their own risk.
*/

#include <avr/power.h>
     
 /* Power Reduction Register (PRR) functions from avr/power.h library. 
 For every disable function there is an enable function
  power_adc_disable() or power_adc_enable()
  power_spi_disable() or power_spi_enable()
  power_timer0_disable() or power_timer0_enable()
  power_timer1_disable() or power_timer1_enable()
  power_timer2_disable() or power_timer2_enable()
  power_twi_disable() or power_twi_enable()
  power_all_enable() or power_all_disable()
  
  Note that for max power efficiency you should also disable the rest of the module for ADC and SPI
   SPCR = 0; //disable SPI
   ADCSRA = 0;  // disable ADC
  */

void setup() {
  delay(6000); //Delay to see normal power level first
  ADCSRA = 0;  // disable ADC by setting ADCSRA register to 0
  power_adc_disable(); //disable the clock to the ADC module
  delay(4000); //delay to see just ADC off power level
  SPCR = 0; //disable SPI by setting SPCR register to 0
  power_spi_disable(); //disable the clock to the SPI module
  delay(4000); //delay to see just ADC and SPI off power level
  power_all_disable();
}

void loop() {
  // put your main code here, to run repeatedly:
}

Tuesday, November 18, 2014

Reducing Arduino’s Power Consumption Part 1

In part 1 of this 3 part series we look at how to use Arduino's built-in sleep modes to drastically cut down on power consumption to help you build projects with longer battery life. In part 2 of this series we will look at how to use power reduction registers and in part three we will look at some other ways to wake an Arduino up from sleep mode.


<avr/sleep.h> library: http://www.nongnu.org/avr-libc/user-manual/group__avr__sleep.html

//**********************Arduino Code***********************************
/*
Example program for using sleep modes in Arduino. This example code was used in a sleep mode tutorial video on the ForceTronics YouTube Channel.
This code is open for anybody to use at their own risk

The 5 different modes are:
     *     SLEEP_MODE_IDLE         -the least power savings
     *     SLEEP_MODE_ADC
     *     SLEEP_MODE_PWR_SAVE
     *     SLEEP_MODE_STANDBY
     *     SLEEP_MODE_PWR_DOWN
These are the arguments used to set the sleep mode in the function set_sleep_mode()
     */
     
#include <avr/sleep.h>

int led = 13; //variable for pin that the LED is on
int count = 0; //variable to control how many times LED blinks before sleep

void setup() {
   sleep_enable(); //enable the sleep capability
   set_sleep_mode(SLEEP_MODE_PWR_DOWN); //set the type of sleep mode. Default is Idle
   pinMode(led, OUTPUT); //set up the LED pin to output
}

void loop() {
  
  if(count < 4) { //For first four loops blink the LED
    digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(900);               // wait 
    digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
    delay(900);       // wait 
    count++; //increment count
  }
  else { //after blinking LED setup interrupt and then go to sleep. Note that sleep will only happen once since it is disabled in ISR
    attachInterrupt(0, interruptFunction, HIGH); 
    sleep_cpu(); //enter sleep mode. Next code that will be executed is the ISR when interrupt wakes Arduino from sleep
    count = 0; //Set the count back to zero 
  }
   
}

//This is the function called when the interrupt occurs (pin 2 goes high)
//this is often referred to as the interrupt service routine or ISR
//This cannot take any input arguments or return anything
void interruptFunction() {
 detachInterrupt(0); //this function call will turn the interrupt off
 sleep_disable(); //Disable the sleep mode so even if call to sleep is executed again it will be ignored
}

Thursday, February 6, 2014

Building a Wireless Temperature Sensor Network Part 1

In this ForceTronics' inaugural project we will be building a wireless temperature sensor network that will include features such as battery powered sensors and the ability to access the temperature data over the internet via a computer or an iOS device such as an iPhone.

The two main building blocks we will be using for this project are an Arduino Board (first the Uno and then later the Yun to add internet capability) and XBee Wireless RF Modules. Now it is assumed that the reader has a basic understanding what Arduino is and some basic experience with the Arduino Uno board, if you do not have Arduino experience don't fret just go to arduino.cc to get started and come back here when you are ready. As for XBee, no experience is necessary we will go over the basics of using XBee here for this project.

Building a wireless temperature sensor network is no easy task so we will break this project up into parts and each part will be covered in a separate post. The project will be broken up into 6 parts, they are as follows:
  1. Introduction, getting started with XBee, and using XBee with Arduino (This post is part 1)
  2. Gathering data from multiple sensors
  3. Design options for powering your sensor network
  4. Design options for powering your sensor network continued 
  5. Monitoring your sensor network over the internet and logging temperature data
  6. Connecting to your sensor network with your iOS device
The focus of the rest of this post will be to learn the basics of XBee and how to use it with Arduino. In an attempt not to reinvent the wheel, we will refer to some fantastic XBee video tutorials created by "tunnelsup." The "XBee Basics" video tutorials is a series made up of 5 video, but you do not need to do all of the videos to complete this project. I am recommended that you actually get the parts and complete Lessons 1 and 4. For lessons 2 and 3 I recommend you watch them and follow along, but you do not need to actually get all the parts and do them (unless you want to). You will not need to do lesson 5 for this project, but if you want to feel free it can only help. Each video provides a parts list of what is needed to complete the tutorial, but if you want to buy in bulk to get you through part 1 and 2 of this project see the parts list at the end of this post.

XBee Basics - Lesson 1 - General Information and Initial Setup

XBee Basics - Lesson 2 - Simple Chat Program Between Two XBees



Hardware you will need for part 1 and 2 of the wireless temperature sensor network project is listed below. Two places where you can buy all this hardware is sparkfun.com and adafruit.com.
  • An Arduino Uno or similar Arduino board. When we add internet connectivity in part 4 to our sensor network we will switch to the Arduino Yun. 
  • Three XBee Modules, ZB Series 2, 2mW with Wire Antenna.
  • Either three XBee Explorers USB from SparkFun or three XBee Adapter Kits from Adafruit (used in video tutorial) for programming and connecting to our XBee modules. If you want to save some money you could just buy two XBee Explorer boards or Xvee Adapter boards, but you will need to buy parts to get your XBee board to plug into a standard breadboard.
  • At least two cables to connect your XBee Explorer boards or XBee Adapter boards to a computer. See the product details for which type cable you need with your board.
  • Three mini breadboards.
  • Three TMP36 or MCP9700 temperature sensors.
  • A power supply that is capable of outputting 3.3 V for powering two of the XBee modules. If you are using the Adafruit Xbee Adapter Kits your power supply can output anywhere between 3.3 V and 5 V since they have an onboard regulator. The third XBee module will be powered by the Arduino board. 
  • Wiring, wire strippers, and a soldering iron
That is it for part 1 of building a wireless temperature sensor network. If you have any questions on what was covered here feel free to asked them in the comments section below or email me at forcetronics@gmail.com. Stay tuned for part 2!



Wednesday, February 5, 2014

Building a Wireless Temperature Sensor Network Part 2

Welcome to part 2 of building a wireless temperature sensor network. In part 1 we learned the basics of how to use XBee and how to use it with Arduino. In this post we will start to build our sensor network and collect temperature data from multiple sensors. The parts list for this post can be found at the end of the part 1 post. Our hardware setup will be very similar to the setup in XBee Basics Lesson 4 video tutorial (found in part 1), except we will add a second XBee router with a temperature sensor and we will add a third temperature sensor to our Arduino board.

Let's get started by installing the proper firmware on our three XBee modules. We want to setup the XBee firmware the same way as in the XBee Basics Lesson 4 video except this time setup two routers instead of one. Be sure to use the same PAN ID for each. Once you have the proper firmware on the XBees let's put the hardware together as shown in the below figure.

Hardware setup for capturing temperature data from multiple XBees
As you can see in the figure, the two routers are tied to the same power supply. That is not very useful for creating a network, but this is just to get started. In part 3 we will look at options for powering each sensor so we can spread them out. You will need to solder a wire or pin to the AD3 hole on the XBee adapter board so you can connect it to the temperature sensor's output on the breadboard. Going forward I will refer to the Arduino and XBee coordinator as the "controller" since it gathers the temperature data from the router XBees, turns it into useful information, and communicates it to the user. Please note that I tied sensor 3 to one of the analog pins on the Arduino so we will use the Arduino to make the analog measurement for calculating the temperature from sensor 3. Having a temperature sensor on the controller is optional. Depending on how you choose to use your sensor network, you may or may not want to make temperature readings at the controller. Below are photos of my setup. The first photo shows the two routers on small red breadboards connected to the TMP36 or MCP9700 temperature sensors. The alligator clips you see in the picture are from the power supply, which is putting out 3.3 V since I am using the Sparkfun Xbee boards (no onboard regulator like the Adafruit versions).

The two routers each with a temperature sensor
XBee coordinator, temperature sensor, and Arduino Uno
The two main differences between this setup and the setup we saw in XBee Basics Lesson 4 is we have two routers sending the controller temperature data and we also have a temperature sensor tied to the controller. That means in the Arduino code we now need to read the address from the received frame of data from each router to determine which router sent it. We also need to read the temperature from sensor 3 and display it to the user. Below is the code for the Arduino with comments to explain what is happening in each line of code. When you go through the code you will want to have the the format or layout of an XBee API RX data frame on hand so you can understand what is happening in the code when it is handling incoming data from the routers. You can get this information from the XBee manual or from the XBee S2 Quick Reference Guide that was in lesson 3 and 4 of the XBee Basics video series. You can get the reference guide from the tunnelsup blog, if you do use it I encourage you to donate $1 to blog. Click here to go access the reference guide.

/*This program was written for the Arduino Uno. The Uno has an XBee Series 2 RF Module connected to it as a coordinator. The Uno uses the XBee coordinator to communicate with two router or end point XBees with temperature sensors. This program recieves the temperature readings from the two endpoint XBees and writes the data to the serial monitor */

/*Each Xbee has a unque 64 bit address. The first 32 bits are common to all XBee. The following four ints (each int holds an address byte) hold the unique 32 bits of the second half of the XBee address*/
 int addr1;
 int addr2;
 int addr3;
 int addr4;
 int sen3Counter = 0; //This counter variable is used print sensor 3 every 5 seconds

void setup()  { 
 Serial.begin(9600); //start the serial communication
}

void loop()  { 
  if (Serial.available() >= 21) { // Wait for coordinator to recieve full XBee frame 
    if (Serial.read() == 0x7E) { // Look for 7E because it is the start byte
      for (int i = 1; i<19; i++) { // Skip through the frame to get to the unique 32 bit address
        //get each byte of the XBee address
        if(i == 8) { addr1 = Serial.read(); }
        else if (i==9) { addr2 = Serial.read(); }
        else if (i==10) { addr3 = Serial.read(); }
        else if (i==11) { addr4 = Serial.read(); }
        else { byte discardByte = Serial.read(); } //else throwout byte we don't need it
      }
      int analogMSB = Serial.read(); // Read the first analog byte data
      int analogLSB = Serial.read(); // Read the second byte
      float volt = calculateXBeeVolt(analogMSB, analogLSB);//Convert analog values to voltage values
      Serial.println(indentifySensor(addr1,addr2,addr3,addr4)); //get identity of XBee and print it
      Serial.print("Temperature in F: ");
      Serial.println(calculateTempF(volt)); //calculate temperature value from voltage value
    }
  }
  delay(10); //delay to allow operations to complete
  //This if else statement is used to print sensor 3 value every 5 second to match the XBee routers
  //It uses the delay() function above to calculate 5 seconds
  if (sen3Counter < 500) { sen3Counter++; }
  else {
    Serial.println("Temperature from sensor 3:");//This is sensor 3
    Serial.print("Temperature in F: ");
    //the following line calculates voltage, then temperature, and then prints temp to serial monitor
    Serial.println(calculateTempF(calculateArduinoVolt(analogRead(A0))));
    sen3Counter = 0; //reset counter back to zero to start another 5 seconds
  }
}

//Function takes in the XBee address and returns the identity of the Xbee that sent the temperature data
String indentifySensor(int a1, int a2, int a3, int a4) {
  int rout1[] = {64, 176, 163, 166}; //Arrays are the 32 bit address of the two XBees routers
  int rout2[] = {64, 177, 63, 221}; 
  if(a1==rout1[0] && a2==rout1[1] && a3==rout1[2] && a4==rout1[3]) { //Check if Sensor 1
    return "Temperature from sensor 1:"; } //temp data is from XBee one
  else if(a1==rout2[0] && a2==rout2[1] && a3==rout2[2] && a4==rout2[3]) {//Check if Sensor 2
    return "Temperature from sensor 2:"; } //temp data is from XBee two
  else { return "I don't know this sensor"; }  //Data is from an unknown XBee
}

//this function calculates temp in F from temp sensor
float calculateTempF(float v1) { 
 float temp = 0;
 //calculate temp in C, .75 volts is 25 C. 10mV per degree
 if (v1 < .75) { temp = 25 - ((.75-v1)/.01); } //if below 25 C
 else if (v1 == .75) {temp = 25; }
 else { temp = 25 + ((v1 -.75)/.01); } //if above 25
 //convert to F
 temp =((temp*9)/5) + 32;
 return temp;
}

//This function takes an XBee analog pin reading and converts it to a voltage value
float calculateXBeeVolt(int analogMSB, int analogLSB) {
  int analogReading = analogLSB + (analogMSB * 256); //Turn the two bytes into an integer value
  float volt = ((float)analogReading / 1023)*1.23; //Convert the analog value to a voltage value
  return volt;
}

//This function takes an Arduino analog pin reading and converts it to a voltage value
float calculateArduinoVolt(int val) {
 float volt = (float)val * (5.0 / 1023.0); //convert ADC value to voltage
 return volt;
}

The next step is to take the code and upload it to the Arduino. Do not forget to disconnect the wire connecting the Arduino digital pin 0 to the XBee coordinator when you upload the code to the Arduino or else you will get an error. Once the code it uploaded, reconnect the wire to digital pin 0 and open the serial monitor. If you have everything setup correctly your serial monitor should look something like the one in the below figure. 

Serial monitor displaying data from each temperature sensor
It is a good idea to place all your sensors in the same area so you know that they should have a similar temperature reading. That way if one of the sensors is off you can easily spot it and investigate what the problem is. Note that these sensors have an accuracy tolerance of +/- 1 degree from the actual temperature so theoretically your sensor readings could differ by up to 2 degrees even if they are right next to each other. If your serial monitor does not look something like the figure above don't worry just go over instructions again to make sure everything is correctly setup, its easy to miss something! If you see the sensor 3 readings in your serial monitor, but you are missing sensor 1 or 2 or both that means one or both of your routers is not sending data to the coordinator. Check the power and communication lights on the XBee adapter boards to ensure they are getting power and are communicating with the coordinator. If they have power, but are not communicating check to make sure you loaded the correct configuration on them.

Well that is it for part 2. Normally I would share the list of parts you need for the next part of the project, but in part 3 and part 4 we will look at multiple options for powering the sensors in the project so read it first and then decide what works best for powering your temperature sensor network. If you have any questions on part 2 use the comment area below or feel free to email your question to me at forcetronics@gmail.com. Stay tuned for part 3!