CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to

An automated LED Plant Light using the PIC12F1840

Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
Arizona Chris

Joined: 20 Dec 2014
Posts: 69
Location: Arizona

View user's profile Send private message Visit poster's website

An automated LED Plant Light using the PIC12F1840
PostPosted: Sun Nov 13, 2016 12:54 pm     Reply with quote

My most recent project uses this wonderful 8 pin PIC that does nearly anything. If you havent looked at this device, then spend some time and check it out.

This project essentially is a bright red/blue LED lamp that turns on for 4 hours at dusk, then delays 14 hours, then looks again for night again. It is a 3 state Finite State Machine that uses a phototransistor to detect the room brightness, and detect either day or night. So far, it works great, and I hope this inspires you not only to try some sort of day/night photocell project yourself, but to consider the possibilities of this awesome PIC.

Ive posted a web page on the project here:

This explains the design, schematics, layout, photos of the project and lists the code.

Here is the CCS-C code directly:


Code for this project using CCS-C into the PIC12F1840 processor:

//Chris Schur
//(Plant Light): 12F1840
//Date: 10/28/16

/*Description: This program will light the high intensity plant light when it
is night time for four hours or so then turn off looking for day again.


//I/O Designations ---------------------------------------------------
// A0 (RA0): Analog input - Photocell
// A1 (RA1): Output - Day/Night Search LED
// A2 (RA2): Output - Day/Night LED
// A3 (RA3): X (can ONLY be input)
// A4 (RA4): Output - Post Delay LED
// A5 (RA5): Output - Lamp ON Array


//Include Files:
#include <12F1840.h> //Normally chip, math, etc. used is here.

//Directives and Defines:

#device ADC=10
#use delay(internal=4000000) //Use internal clock, no Xtal
#use fast_io(A)

//Global Variables:
int16 result; //0 - 1023 out of ADC
int8 fsm; //finite state
int16 n; //time counting var.

//-- Main Program

void main(void) {

// Set TRIS I/O directions, define analog inputs, compartors:
//(analog inputs digital by default)


//(analog inputs digital by default)

//Initialize variables and Outputs: --------------------------------------


fsm = 0; //initial state upon turn on



while(true) {

//FSM for operation
switch (fsm) {

//Here we measure the photocell, if its day we stay here, if its night
//we jump to state 1.

output_low(PIN_A4); //turn off post delay lite

//First we read the photocell:
result = read_adc(); //take analog reading. 5v=day, 0v=nite

if (result >= 10) { //daytime
delay_ms(1000); //take readings periodically lookin for night
output_low(PIN_A1); //blink night search LED

fsm = 0; } //stay here until night, keep looking.

if (result < 10) { //nighttime
fsm = 1; } //jump state, its night now.


case 1: //LIGHT ON STATE
//Simple - turn on the big lamps for 4 hours, then jump to state 2
output_low(PIN_A1); //no longer searching for night
output_high(PIN_A5); //turn on plant light

//now 4 hour delay:
//delay_ms(3000); //short test delay of 3s

//for(n=1; n<2; n++) { //1 mins test delay

for(n=1; n<240; n++) { //240 mins For 4H delay
delay_ms(60000); } //one minute delay = 60,000 mS.

//turn off plant lamp:

fsm = 2; //jump state to post light long delay...


//Here we delay after lamps turn off until it will be day time (~14h)
//then jump back to state 1.
output_high(PIN_A4); //post delay lamp

//14 hour delay now:
//delay_ms(5000); //5s test delay

//for(n=1; n<2; n++) { //1 mins test delay

for(n=1; n<840; n++) { //840 mins For 14H delay
delay_ms(60000); } //one minute delay = 60,000 mS.

//ok, its daytime now, youve waited long enough

fsm = 0; //done, jump back to light measure state look for night.

} //switch

} //while


} //main
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Powered by phpBB © 2001, 2005 phpBB Group