GAMING FUN
Published © GPL3+

Fan speed controlled by light

Controlling the speed of fan by the light intensity

BeginnerShowcase (no instructions)178
Fan speed controlled by light

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Axial Fan, 12 VDC
Axial Fan, 12 VDC
×1
Motor Driver/Controller, H-Bridge
Motor Driver/Controller, H-Bridge
×1
5 mm LED: Red
5 mm LED: Red
×8
LDR, 5 Mohm
LDR, 5 Mohm
×1
Male/Male Jumper Wires
×1
Breadboard (generic)
Breadboard (generic)
×1

Code

c code

C/C++
#define F_CPU 16000000
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#define MOTOR PB2
#define led0 PD0
#define led1 PD1
#define led2 PD2
#define led3 PD3
#define led4 PD4
#define led5 PD5
#define led6 PD6
#define led7 PD7
#define DTOA PC2
#define MOTOR PB3

volatile unsigned int adcValue=0;
volatile unsigned int ledValue=0;
ISR(ADC_vect)
{
    // TIFR1=(1<<OCF1A);

//if((ADCSRA & (1 << ADSC))!=0); /* wait until done */
adcValue = ADC; /* read ADC in */
/* Have 10 bits, want 3 (eight LEDs after all) */
ledValue = (adcValue >> 2);
/* Light up all LEDs up to ledValue */
//PORTD=0;
 // _delay_us(1000);
PORTD = ( ledValue);
 // _delay_ms(1000); 
  ledValue=(float)((ledValue/255.0)*100);
OCR2A=(unsigned int) ((ledValue/100.0)*0xff);
_delay_ms(2000);
//TCNT2=0;


}






void setup() {
  
   cli();
  ADMUX |= (1<<REFS0)|(1<< MUX1); /* reference voltage on AVCC */
   ADCSRA |= (1 << ADPS1) | (1 << ADPS0)|(1<<ADPS2); /* ADC clock prescaler /8 */
   ADCSRA |= (1 << ADEN)|(1<<ADIE)|(1<<ADATE); /* enable ADC */
  ADCSRB |=(1<<ADTS1) |(1<<ADTS2);
  ADCSRB &= ~(1<<ADTS0);

   TCCR1A = 0 ;     // initialize to zero both control registers
    TCCR1B = 0;     // In most cases, no need to write to TCCRC1
    TIMSK1 |= (1<< TOIE1 ) ;
         // disable global interrupts so that no interrupts occurs while configuring
    
    TCCR1A |=  (1<< WGM10)| ( 1 << COM1B1)  ;  // set wave-generation mode using TCCRA1 and TCCRB1. Check datasheet table to choose appropriate mode
 TCCR1A &= ~(1 << COM1B0) ;
  TCCR1B |= (1 << WGM13)|(1<<WGM12);     // note that you might need to set certain bits to 1 and others to zero in both registers
   
      TCNT1 = 0; 

  ICR1 = 63;
  

TCCR1B|=  (1 << CS12);



TCCR2A = 0 ;     // initialize to zero both control registers
    TCCR2B = 0;     // In most cases, no need to write to TCCRC1
    
         // disable global interrupts so that no interrupts occurs while configuring
          TCNT2= 0; 

    TCCR2A |=  (1<< WGM20) |( 1 << COM2A1)  ;  // set wave-generation mode using TCCRA1 and TCCRB1. Check datasheet table to choose appropriate mode
 
  TCCR2B |=(1<<CS21)  ;     // note that you might need to set certain bits to 1 and others to zero in both registers
   

 




   //int frequency=5000;
      // ICR1 can be used to store the top value is some modes
  
     // initialize counter by writing to TCNT1. Later when timer is turned on, counter will start from this value

  
    
    sei(); 
      ADCSRA |= (1 << ADSC);
  
DDRD = 0xFF;
DDRC &= ~(1 << DTOA);
DDRB |=(1<<MOTOR); 
}

void loop() {
  // put your main code here, to run repeatedly:
     
PORTB |= (1<< PB2);
        PORTB &= ~(1<<PB0);
  
  

}

Credits

GAMING FUN
3 projects • 0 followers
Contact

Comments

Please log in or sign up to comment.