Skip to main content

more options

Smart House

Smart House

Julia Sciarello, Mauricio Munoz, Carla Quiller

Photo of circuit.



Our project implements a smart algorithm in order to power a house with a photovoltaic, batteries or the power grid.  For this project, we worked closely with a research team whose goal is to power a home with minimal power from the power grid.  In order to form this smart home, we needed to monitor the voltage and current flow from each of the sources (photovoltaic, batteries, and the grid) and the home.  We implemented these current and voltage monitors.  The next step was to come up with an algorithm that would determine what source should be powering the house and when the battery should be charged.  The final step was to send out data to a home display module so that the data can be analyzed.

High Level Design

Our project idea came from a research team focusing on building a smart home, since one of our group members is working with the team.  In order to decide how to power the house, it is necessary to find the power demand of the house and the available power from each source.  The power can be found by the following equations: P=IV, P=V^2/R, P=I^2R. 
For AC power calculations we needed to find average power instead of instantaneous power. Therefore we needed to find the root-mean-squared value for the current and the power, and we also needed to find the phase difference between the voltage and the current.

V(t)=Vpeak*cos(2πft + φ)
Pavg=I*V=Irms*Vrms*cos(φ) => Vrms=Vpeak/√2 and Irms=Ipeak/√2

For our project we are using the HCPL-7520 Linear Opto-isolator in order to determine the voltage drop across a resistor. These isolators can be modeled by the equation below, where Vin is the differential input.


The voltages that we are measuring exceed 5V, therefore we used voltage dividers to scale down those voltages. For calculations, we found the actual voltage on the line by using the following equation, where the input to the microcontroller would be Vscale and the actual voltage would be Vhigh.


The diagram below shows the layout of the power sources, relays, and the load. The solar panel generates DC power and when Relay 1 is closed, that power is sent to an inverter, which transforms the power to AC. The inverter outputs a 120AC signal, which is then stepped-down to 12VAC using a transformer for safety purposes. The 12VAC is then sent to the home. The nickel-cadmium batteries generate DC power, which is also sent to the inverter when Relay 3 is closed in order to power the home. These batteries can be charged by the grid when Relay 4 is closed and by the photovoltaic when Relay 2 is closed. A DC to DC converter is needed when charging the battery with the photovoltaic in order to charge the battery properly. When charging the batteries with the grid power, we need to transform the AC power into DC. We achieved this by passing the signal through a full-wave rectifier and a DC to DC converter. In order to power the house with the grid, Relay 5 must be closed. In order to determine which relays are closed we monitored the available power from the battery and the PV. Using the power measurements we ran an algorithm that decided the most efficient way to power the house.

This is the overall layout of our circuit.

Software/Hardware Tradeoffs

Some software tradeoffs we made involved our calculations of the voltage, current, and power.  When using a microcontroller, fixed-point math is the best way to go to make sure the calculations get done as quickly as possible.  However, we are more concerned with the accuracy of these numbers and chose to use floating point instead.  Since we are only doing major calculations every ten seconds, we were not concerned with the delay this floating-point arithmetic would have on our design.  Another tradeoff that we made was to step down the voltages using a current divider in order to measure current and voltages.  This is because we are using 12 V, and the opto-isolator we used could only handle inputs of up to 5V.  We saw that a previous project had used this opto-isolator successfully so we decided to use it and risk having a small percent of error from scaling down.  We decided to use TinyRealTime instead of interrupt service routines because we had to accurately schedule our tasks due to deadlines. Another tradeoff involved finding the charge of the batteries. Instead of following the discharge behavior of the batteries or integrating the current, we chose to simply measure the voltage and divide this voltage by the maximum voltage of the batteries to find the charge percentage. We could do this because nickel-cadmium has a nearly linear relationship between charge and voltage. This is a software tradeoff because it saved us time in calculations and proved an approximate result.

      In terms of hardware tradeoffs, we had to decide how to accurately measure current without losing power to the house. We had two options: use a current divider, an opto-isolator, and an operational amplifier or use a current-sensing resistor and a differential opto-isolator. We attempted to use the first option, however the opto-isolator needed 1.8V across it. This voltage drop results in a decrease in power delivery to the house. Choosing the proper resistance values to get this 1.8V drop was difficult. The output of the op-amp was a voltage that we used to find the current flowing through the house, but we could not find the appropriate scaling factor between the voltage and current. The voltage output was on the milli-volt range, so we did not think the ADC converter would understand how to scale this small voltage. Therefore we decided to use the second option, which will be described in the Program Detail section.
There are many products on the market for saving energy and measuring the power consumption of your house.  There are even companies that are devoted to producing the “Smart Home” we are trying to create.  IEEE standards for sizing nickel cadmium batteries for a photovoltaic, IEEE standards for utility interface of residential and intermediate photovoltaic systems.

There are many products on the market for saving energy and measuring the power consumption of your house.  These products include the Kill-A-Watt and various other surge protector based power conservation devices.  There are even companies that are devoted to producing the “Smart Home” we are trying to create.  For this type of product, we must satisfy IEEE standards for sizing nickel cadmium batteries for a photovoltaic as well as IEEE standards for utility interface of residential and intermediate photovoltaic systems.

Program Details 

Our code consisted of four main tasks.  The first task, getInput(), is a continuous task used to monitor the inputs from the user.  In order to initiate the program, the user is required to input the start time (hour in military time, minute, second) for accurate time keeping.  This function then continuously watches for inputs from the user in order to signify a demand response event has occurred.  A demand response event is one in which the power company would signal that the home needs to reduce it’s power consumption.  In order to represent this, the user would input the maximum power the house can use and the length of this power restriction.

The updateClk() task simply keeps track of time.  It has a release and deadline time of 1.0 second and increments the second of the clock.  When the second reaches 60, it is reset to 0 while the minute variable is incremented.  The minute variable is incremented until it reaches 60 as well, where it is reset and the hour variable is incremented.  The hour is in military time, so instead of rolling over to 24 it will reset to zero.  Time keeping is very important in our project because we need to keep track of the price of energy, which varies throughout the day.  The price of energy plays a role in our decision-making algorithm.

PVmonitor() is the monitoring task that calculates the power of the PV, power demand of the home, the battery charge, and the power being produced by or sold back to the grid.  To get the power, we need to find the voltage and current for each source.  We do this by reading in the outputs of the four opto-isolators into the ADC on the microcontroller, as well as the voltage from our voltage dividers.  The voltage is calculated based on the voltage divider, as well as a calibration factor.  In order to find the current, we calculate the actual voltage drop across our 2Ω current sensor by using the equation from the opto-isolator and a factor from our voltage divider. 


The ADC is an analog-to-digital converter, which takes in an analog value and converts it to a number between 0 and 255. We can retreive the original analog number by multiplying the ADCH number by 5/255, where 5 is the reference voltage. 

Then we use Ohm’s Law to get the current by dividing Vdiff by 2Ω.  The power can then be calculated by multiplying this current by the previous voltage we found.
In order to get the AC power, we measured the current 1000 times in order to find the peak current.  Once we found the peak current, we could calculate the Vrms by dividing the peak current by √2.  When looking at the output signals of the opto-isolator and the AC power signal, we saw that there was no phase difference.  This means we can simply multiply Vrms and Irms together to get the power.  After collecting all the power data, we sent it out using the RS232 and Uart. 
In case the PV is not in use, we have a check that occurs every ten minutes so that we can get the power of the PV.  To do this we close the relays so that the PV can charge the battery, and we can get a power reading for it.  Otherwise we wouldn’t know how much the PV can supply since there isn’t current flowing through the resistor at all times.  After the measurement has been made, we turn off the PV and return the circuit to its previous state.  If the PV is supplying enough power, the algorithm will decide to turn it back on.

Algorithm() was the biggest portion of the project.  We sat down with the research group in order to discuss the most efficient way to power the home under various circumstances.  There are two main conditions in this algorithm: whether or not there is a demand response signal from the utility.  If there is no demand response signal, our algorithm progresses in the following order:

  • If the battery is at 20% of full charge or less, perform an emergency charge back up to 75% and proceed through the rest of the algorithm
  • If PV power is greater than the power demand of the home, let the PV power the house
  • If PV power and battery can provide enough power for the home, then they power the house
  • If PV power is greater than the home demand, battery charge is greater than 80%, and the price of energy is in the upper 50%, we sell energy from the PV back to the grid
  • If PV power is greater than 70% of home demand, it’s before 12 PM, the price of energy is in the lower 50%, and the battery is less than 95% full, we power the house with the grid and charge the batteries with the PV.
  • Otherwise, just power the house with the grid.

If there is a demand response signaled, then the algorithm proceeds in the following way:

  • Check to see if the power from the PV and the battery exceed the power demand
  • If so, proceed to check if the PV and battery can sustain the home’s power demand for the length of the demand response
  • If so, check to see if the PV power is greater than the power demand. 
    • If it is, sell back power to grid.
    • Otherwise, use the grid to power the house and PV to charge the battery
  • If not, use the grid to power the home and charge the battery with the PV.

We keep track of the time, so that once the demand response is over we can return to the original algorithm.

The trickiest part of our project was trying to measure current.  The opto-isolators we used were very sensitive to voltage changes and for some reason our voltage dividers did not work quite as expected.  In order to calculate the correct current, we needed to measure multiple times in order to find the correct scaling factor between the voltage output of the opto-isolator and the actual voltage drop across the current sensing resistor.  It was also frustrating that for each of the four current sensing circuits, the scaling factor did not remain the same.  When measuring AC signals, we needed to make sure that we were getting the peak value for current in order to calculate the average power.

Hardware Details

Our project consisted of a few different circuits.  Since we couldn’t demo with a real photovoltaic, it was necessary for us to build a circuit that effectively represented the exponential I-V curve of a photovoltaic.  We found the circuit seen below from a research paper (see appendix).  It can be made with a current source, which we made using an op-amp, three LEDs and a couple of resistors.  As the current increases, the voltage output does as well.  Because the LEDs are different colors and have different “on-voltages”, they will turn on at different times, providing three different linear regions in the I-V curve.  This creates a somewhat exponential effect for the current.
Photo of circuit.

Our current monitoring circuits can be seen in the pictures below.  We have one design for the DC voltages of the battery and PV and another for the AC signals of the grid and the home.  The only real difference is the resistors used for the voltage divider.  For both circuits, a signal comes through the 2Ω resistor, and the voltages are scaled down on both sides of the resistor.  For the DC circuit, we scaled the voltages down by a factor of ten and the AC signals were scaled down by a factor of four.  The scaled down voltages on both sides of the resistor get sent into the opto-isolator, which sends a voltage to the ADC of the MCU that corresponds to the voltage difference of the two inputs.  This voltage is then used to find the current through the resistor.  We also send the scaled down voltage on the output of the resistor to the MCU so that we can calculate the voltage on the output of the PV and calculate the power. 
Photo of circuit.


The other part of our circuit includes the five relays.  Each of these relays has four connections.  Two are for the MCU ground as well as a signal from a pin on the MCU and the other two are for the two connecting or disconnecting the power source to the rest of the circuit.  When the MCU sends out a 5V signal to the relay, the switch will close allowing current to flow through the circuit. 


Photo of circuit.

In doing research for this project, we’ve been closely referencing the ECE 4760 PowerBox project from a couple years ago.  They used the same opto-isolators we are using and a similar power averaging technique.  We did not copy their design or code, but did use it as a reference for our circuit and code. 
Over the course of this project, we have tried several different ways of measuring current and sending data.  We had originally tried to use an H11F1 opto-isolator along with a current divider and an LM 358 operational amplifier as described earlier, however we were not able to effectively determine the I-V curve.  We did some research and found the PowerBox project and decided to attempt their method of measuring current.  As for sending data, we had set up the SPI port so that we could send data via pin B.5, but we realized that for the amount of data we had to send it would take a large amount of time to transmit.  The receiver would also have to decode our message in a specific way.  We did all of our calculations in floating-point so they were more accurate, but in order to send them over this SPI port we would have had to convert them to fixed-point.  This may have caused some inaccuracy in the readings.  After talking to the other team that is working with our research group, we decided to just print the data out through pins D0 and D1.  That made it easier for us to send data and for them to receive it.


Results of Design


For our design, we were not concerned with the speed of the execution as long as all of the tasks were executed by the deadlines and as long as all the calculations could get done by the deadline.  Because our main tasks execute every 10 seconds, we were able to use floating-point calculations without running into timing issues.  We were not able to notice any sort of hesitation or flickering within our program.  It seems to work as desired.
In terms of accuracy, we used voltage and current values from the multi-meter to compare with the calculated voltages and currents. We then applied an appropriate calibration factor to achieve the right voltage and current. However, these scaling factors varied by device, workstation, and randomly; therefore our calculations will always have some error associated with them.  
Initially, the research wanted us to scale the power to the full 120VAC. However we decided that this voltage was too dangerous to be handling in lab. Therefore we used 120V to 18V transformers. We also used isolators to separate the power sources to ensure that the microcontroller is protected from voltages exceeding 5V. Our design was safe and did not interfere with other design projects.
We aimed to make this device as user-friendly as possible. Therefore the product only needs to be installed and initialized, and then the program will run by itself. In the real world, the utility company, not the user, will issue any demand response signal directly to the device. The only input needed from the user is the time of day at the initial set-up.


Actual (V) Code Measurement(V) Error (%)
10.38 10.364 .154
14.07 13.968 .73
4.34 4.281 1.38


Actual(mA) Code Measurement(mA) Error(%)
4.45 5.11 14.9
7.8 7.14 8.42
8.5 9.17 7.30



We changed our design several times. We wanted to use SPI, however that was too time consuming. Instead the RS232 proved to be more simple and efficient. If we were to do this project again, we would have used opto-isolators that could handle higher voltages, so that we would not have had to use voltage dividers. This would have decreased the error in our current and voltage calculations. Another feature we would have liked to implement is weather forecasting. If our algorithm function could receive the daily weather updates, we would be able to more accurately predict the power from the PV, which would be useful in demand response situations. The behavior of the nickel cadmium batteries did not meet our expectations. The discharge rate was faster than we anticipated and the charging rate was slower than expected. Therefore we could not incorporate the batteries into our demo. Instead we used another power supply to simulate the battery voltage.
We did not use any other code. However we did use a similar design for calculating current and voltage as in the SmartBox design. We also referenced code written for the ECE 4760 lectures for using SPI, ADC, and TRT. This project was not aimed at reverse-engineering a patented or trademarked design, but there are many people who are working on solving the same problem.  Power conservation is a very significant issue in America. Our project utilizes environmentally-friendly power sources, which decreases the dependence on the power grid. Therefore our project has the possibility of getting a patent or being published.

We did our best to follow the IEEE standards for sizing Nickel Cadmium Batteries for use with a Photovoltaic as well as IEEE’s recommended practice for the Interface of Residential and Intermediate Photovoltaic Systems.  Our research group had already picked out the batteries and the photovoltaic for us, so they had looked into this.  In making our circuits we had to make sure our PV wouldn’t have excessive voltage flickers and we also made sure that the power from the PV was converted to 60 Hz AC, to match the grid’s AC frequency.  For the batteries, our code made sure that if the battery supply dipped below a certain value, we would have an emergency charge to bring the battery power back up to a certain percentage. 


Ethical Considerations

While working on this project, we followed the IEEE Code of Ethics.  While we referenced other projects and informational sites, we wrote our own code and gave all credit where it was due.  Our group has always tried to help other students in the lab, especially the other group who was trying to use the same opto-isolator as us.  Our ECE 4760 class consists of students of all races, genders, and religions, and we have treated each and every one of them fairly. 
On our report we have accurately portrayed our design and our results, as to give an honest review of our project.  We also sought out help for circuit design because there were many ways to implement current and voltage measurers. We respected the property of the other students by not touching things that weren’t ours, by following safety rules, and by avoiding horseplay in the lab. Safety was our highest priority, which is why we used transformers to step down the AC signal from 120V to 18V. We had to solder the opto-isolators, so we used goggles to protect our eyes.
Throughout this project we learned how to properly opto-isolate, use SPI, measure current and voltage, rectify AC signals, operate relays, and make current sources. Therefore we broadened our knowledge of circuit design and the capabilities of microcontrollers.

Legal Considerations

Our project is just for research purposes in order to simulate a home, so there are no legal considerations.


  • Final Project Code
  • Appendix: Schematics


    This is a simulation circuit of the photovoltaic

    The current-sensing and voltage measuring circuit
    Appendix: Cost

    Product Quantity Cost Each Total
    Avago Technologies HCPL 7520  4 $6.02 $24.08
    Inverter from research group      
    2 Transformers from Research Group      
    Power Supply 1 $5.00 $5.00
    White Board 5 $6.00 $30.00
    STK500 1 $15.00 $15.00
    Ni-Cad Batteries      
    2 Ohm Resistors from Lab      
    LM358 Op-Amp from Lab      
    Various Capacitors and Resistors from lab      
    Total     $74.08

    Appendix: Tasks

    Julia Sciarello

    • Circuit design and experimentation
    • Software coding
    • Testing and debugging
    • Report
    • Soldering

    Carla Quiller

    • Circuit design and experimentation
    • Software coding
    • Testing and debugging
    • Report
    • Webpage creation
    • Soldering

    Mauricio Munoz

    • Circuit design and experimentation
    • Testing and debugging
    • Soldering
    • Software coding
    • Webpage creation



  • Datasheet: FairchildLM 358
  • Research paper on PV simulator circuit
  • AC Resistor Behavior
  • Avago Technologies HCPL-7520 Linear Optoisolator
  • ECE 476 Website
  • Wikipedia AC power article
  • Atmel Mega644 Microcontroller Datasheet
  • PowerBox
  • Bruce Land