[Dimmer] Amplified current feedback

Opamp 741 negative feedback pot 503 over 1k2 input resistor.

Negative direction load current.

Average.h 200 samples, Timer 1 frequency 50Hz.

Need noise filter.

Next time I will use digital potentiometer instead of pot 503 and automatic gain adjust algorithm.

acs712_amplifier_741

R3 = 1k2, R4 is replaced with POT 503 and use Op-amp 741

[Code] c_dimmer_timer.ino


/*
 * Date: Tue, 25/04/2017
 * Desc: Serving for mono_dimmer 
 * Info: PWM on pin 5, positive logic [255] = brightest, [0] = off
 * Timer 0 is used for pin 5 PWM
 * Timer 1 is used for get adc sampling time with Ts = 20ms (for 50Hz)
*/
#include <Average.h>
// Setting array for calculation
int lengthArray = 200;
Average<float> ave_nor(lengthArray);
Average<float> ave_amp(lengthArray);

int startMark = 0;
const int ledPin = 5;

int value = 150;
void setup()
{
 //pinMode(ledPin, OUTPUT);
 setPwmFrequency(5, 8);
 pinMode(2, INPUT);
 Serial.begin(9600);
 Serial.println("On going");

analogWrite(ledPin, value);
 // Using timer 1 interrupt
 noInterrupts(); // disable all interrupts
 TCCR1A = 0;
 TCCR1B = 0;
 //TCNT1 = 16242; // preload timer 65536-16MHz/256/1Hz
 //TCNT1 = 34286; // preload timer 65536-16MHz/256/2Hz
 TCNT1 = 64286; // preload timer 65536-16MHz/256/50Hz
 TCCR1B |= (1 << CS12); // 256 prescaler 
 TIMSK1 |= (1 << TOIE1); // enable timer overflow interrupt
 interrupts(); // enable all interrupts
}

ISR(TIMER1_OVF_vect) // interrupt service routine that wraps a user defined function supplied by attachInterrupt
{
 TCNT1 = 64286; // preload timer
 // Get the data;
 ave_nor.push(analogRead(A2));
 ave_amp.push(analogRead(A1));
 startMark++;
 if (startMark == lengthArray){
 float sensorValue_a = ave_amp.mean();
 float sensorValue_n = ave_nor.mean();
 Serial.print(sensorValue_a);
 Serial.print("-----");
 Serial.println(sensorValue_n);
 // reset
 startMark = 0;
 } 
}

void loop ()
{
 if (Serial.available()) {
 char ser = Serial.read(); //read serial as a character
 
 //NOTE because the serial is read as “char” and not “int”, the read value must be compared to character numbers
 //hence the quotes around the numbers in the case statement
 
 switch (ser)
 {
 case '0':
 value = 0; 
 break;
 
 case '1':
 value = 100; 
 break;
 
 case '2':
 value = 130; 
 break;
 
 case '3':
 value = 145; 
 break;
 
 case '4':
 value = 160; 
 break;

case '5':
 value = 175; 
 break;

case '6':
 value = 190; 
 break;

case '7':
 value = 210; 
 break;

case '8':
 value = 230; 
 break;

case '9':
 value = 255; 
 break;
 
 default:
 //Serial.println("Invalid entry");
 break;
 }
 }
 analogWrite(ledPin, value);
 //
}

void setPwmFrequency(int pin, int divisor) {
 byte mode;
 if(pin == 5 || pin == 6 || pin == 9 || pin == 10) {
 switch(divisor) {
 case 1: mode = 0x01; break;
 case 8: mode = 0x02; break;
 case 64: mode = 0x03; break;
 case 256: mode = 0x04; break;
 case 1024: mode = 0x05; break;
 default: return;
 }
 if(pin == 5 || pin == 6) {
 TCCR0B = TCCR0B & 0b11111000 | mode;
 } else {
 TCCR1B = TCCR1B & 0b11111000 | mode;
 }
 } else if(pin == 3 || pin == 11) {
 switch(divisor) {
 case 1: mode = 0x01; break;
 case 8: mode = 0x02; break;
 case 32: mode = 0x03; break;
 case 64: mode = 0x04; break;
 case 128: mode = 0x05; break;
 case 256: mode = 0x06; break;
 case 1024: mode = 0x07; break;
 default: return;
 }
 TCCR2B = TCCR2B & 0b11111000 | mode;
 }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s