Software Design

Wireless Ambient Lighting Control System

Pratik Panchal (, Kedar Vidvans (
M.Eng, School of Electrical and Computer Engineering,
Cornell University

High Level Design  |  Hardware Design  |  Software Design  |  Results  |  General Issues  |  Appendix  |  My HomePage

Software Design:

Software design for this project is divided into four main parts:
1. Wireless interface
2. Control in automatic mode
3. Control in manual mode
4. PIR sensor

Wireless Interface: As mentioned in the hardware section and introduction, we had used 433Mhz OOK transmitter receiver pair for wireless communication. These modules use Amplitude Shift Keying (OOK) for modulation. The presence of a carrier for a specific duration represents a binary one, while its absence for the same duration represents a binary zero. The demodulator then recognises the pattern and reconstructs the data. This type of wireless transmission is prone to distortion due to noise, hence we had undertaken software as well as hardware based measures to keep this in check. The hardware checks are described in the hardware section so lets move to the software part.

There will always be loss and mutilation of packets in wireless transmission, the trick is to recognise this and take steps to come up with a method of checking the data as it arrives. We could have gone for various encoding methods such as NRZ or manchester but we decided to use USART because of ease of implementation and prior experience using it.

Transmission: The reason that the module is prone to noise is that the receiver will increase its gain to the maximum level in case it does not notice a signal hence start picking up noise. We have to account for this and tune the receiver before sending any data. The tuning can be achieved by sending a stream of 1’s and 0’s. This way the receiver can be synchronized to the transmitter. We sent a constant stream of 0xAA to to the receiver for synchronization by sending 6 synchronization bytes. Next we sent the address byte of the module followed by the data byte, which was the luminosity measured by the sensor. Then we need to transmit a checksum, which tells the receiver that the data sent in the previous transmission is correct and not noise. In our case, the checksum was the sum of the data byte and address byte 0xAA. The following flowchart, in figure 8, depicts that.

Figure 8: Transmission side flowchart

The receiver side code matched the transmitted and generated checksums and validated the data only if the checksums were equal. We have taken a reference for the transmitter and receiver code from a WINAVR tutorial whose link we found on a product page of sparkfun. We have made changes to this code to suit our needs. The following flowchart, in figure 9, depicts that.

Figure 10: Reception side flowchart

2. Control in Automatic Mode: The user gives a set point to the system and is stored in the control unit as luminosity_set. This is done by the increment/decrement buttons, in totality of 11 steps. The first and last steps force PWM module to switch off and make the pin low / high to make the lamp completely off or on at full intensity. This is done because we observed that there was a very small flickering even at OCR0 value of 0. Similarly, at full value of OCR0, we saw that there was a spike of very narrow width in the PWM output. 

The control unit tries to maintain the luminosity near this set point. The sensor MCU measures the resistance of the light sensor through the ADC. the ADC has been configured to give 8 bit resolution.The code for measuring resistance through the ADC was already developed in lab-2. This measured resistance is inversely proportional to the amount of light falling on it. This 8-bit data is directly transmitted to the receiver-end control unit.

The control unit, then, compares this received data to the luminosity_set, set by the user, if the measured luminosity falls outside a specific range of luminosity_set value, a control action is taken and the PWM is incremented / decremented in steps of PWMsteps. 

A suitable selection of this 'range' value is necessary. An extremely narrow range value will make the system extremely sensitive and any slight obstructions on sensor will initiate the control action. The temperature changes will also cause the execution of control algorithm at low ranges. A higher value will make the system extremely insensitive to luminosity changes. A selection of this parameter depends on the ambience and the nature of change of luminousity observed. Another critical parameter is PWMsteps, the proper selection of which is important to achieve a non-oscillatory control action. A too low value of PWMsteps will make the control extremely slow whereas a higher value will make the control oscillatory. We chose the value of PWMsteps such that there were no oscilllations in PWM stable point. We did loose somewhat in the speed, but we believe it is a better option since the slow change in luminous output of lamp does not strain the human eye and thus avoids instantaneous opening/closing of eye lens (iris). A flow chart to demonstrate this is shown in figure 10.

Figure 10: Control algorithm for auto mode

The code for ADC conversion is given below:

//initialize ADC
ADMUX = (1 << ADLAR) | 0b01000000;
ADCSRA = (1<<ADEN) | (1<<ADSC) + 7 ;
while ((ADCSRA & (1 << ADSC))); //check if conversion is complete or not
luminosity=  ADCH; //has a range of 0-255 ,8 bit 
ADCSRA |= (1<<ADSC) ; //starts next ADC conversion

Our control is based on the fact that humans cannot perceive small differences between light intensities, hence we can allow for a larger margin of error between the set point and the measured luminosity.

3. Control in Manual mode: In the manual mode, the user, directly controls the intensity of lighting. The sensor feedback is decoupled in this case. the user can increment or decrement the luminosity using push buttons on the control unit in steps of 11. The functionality is similar to what introduced in previous section, except the automatic control. A flow chart to demonstrate this is shown in figure 11.

Figure 11: Control algorithm for manual mode

4. PIR Sensor: The PIR sensor as mentioned earlier detects presence of humans in the room. We have used a PIR sensor that provides a high in the output when human presense is detected. We scan the port pin connected to the output of the PIR sensor every time the program is executed. If the PIR sensor gives a zero then we had timer2 running which would timeout after 1 minute (for demonstration only, practical system will have a larger timeout). The PIR sensor has to continuously give a zero of the timer to time out. If an intermittent 1 is given by the PIR then the timer is reset. A flow chart for this is shown in figure 12.

Figure 12: PIR sensor control algorithm

The code for the timer2 is: 

#define t1 30000 //Light time out set to 1 min->30000 
TIMSK | =(1<<OCIE2);//|(1<<TOIE2);  
OCR2 = 249;   //set the compare reg to 250 time ticks which is 2 msec for 8 Mhz crystal

//Update the task time
if (time1>0)  --time1;

if (time1==0 && PIR==1)
{time1=t1; }
if (PIR==1)
DDRB = (1<<PINB3);

{DDRB = (0<<PINB3);}

Note that the TIMSK register is common for all the timers r unlike ATMega644 where each timer has a dedicated TIMSK register.

UART Transmission: We used modules from Radiotronix for this. These modules are OOK modules working on a frequency of 433 Mhz which acceptable under the FCC regulations for intermittent control signals. The baud rate for these modules has a maximum limit of 4800. But, in order to keep the effect of noise low, a lower baud rate is needed. We decided to limit our maximum baud rate to 300 due to this. The baudrate to be set goes into a calculation to decide on a value to be set in UBRRH and UBRRL register. The formula is = (crystal frequency/(baud rate * 16)) -1.

For a baudrate of 300, this figure comes to 52. this value is given an input to UBRRL register and the UBRRH register is set to zero(because 52 < 255). Next, we set the UPM0(1:0) bits in UCSRC register to 11 to set the UART module in odd parity mode, and USCZ(1:0) to 11 to make the character size eight bit. To send a byte to a radiofrequency transmitter from the MCU, first we neec to ensure that the UART transmit buffer was empty by checking UDRE bit in UCSRA register. Once the transmit buffer was empty, then we wrote byte to UDR register. 

UART Reception: The UART reception works on interrupts. Hence, we have to set the RXEN and RXCIE bit to 1 in the UCSRB register. The rest of the configurations remain the same as that on the transmitter.  We also had to write an Interrupt Service Routine (ISR) which was called when the UDR register (data reception) became full. This ISR reads the UDR and does the filtering process to validate if the data received is true or not.

Light Control: As mentioned earlier, we used PWM to control the intensity of the LED bulbs. The frequency and hence the pre-scalar of the PWM has to be high enough that, the end user does not notice any blips in the LEDs and not too high that it is above the band width of the opto isolator used to isolate the MCU from the LEDs power supply. hence we settled for a pre-scalar of 64 to the clock frequency for the PWM. Some thing to note is that as opposed to ATMega644 where two registers TCCRA and TCCRB have to be configured to set the PWM , for ATMega16 only on register TCCR has to be set. The code for setting this is as follows:

(1<<WGM00)|(1<<WGM01)|(0<<CS02)|(1<<CS01)|(1<<CS00) ;

The reception and transmission waveforms are captured on channel 1 and 2 respectively and shown in the oscilloscope screenshot in figure 13 below:

Figure 13: Oscilloscope screenshot of transmission and reception waveforms

Disclaimer: This work has been done as term work for the course ECE4760 Digital System Design using Microcontroller, at school of Electrical and Computer Engineering, Cornell University by Pratik Panchal and Kedar Vidvans under guidance of Prof. Bruce Land. Readers can use the presented work on this site as long as they acknowledge the source. The work is presented in as-is condition and If used, no liability is borne by either the authors or the school.

web counter