Introduction: ATTiny85 Imazing and Simple Way to Control AC Lamp
ATtiny85 is one of the popular MCU from Atmel AVR family,this MCU is 8 pin only , small and Low cost chip,on the internet there are dozens of project based on ATtiny series.
The good thing about ATtiny85:
- Have 5 pins I/O , 2 digital Pins can work as PWM
- 3 analog pin
- Support most of arduino commands like:
- pinMode()
- digitalWrite()
- digitalRead()
- analogRead()
- analogWrite()
- shiftOut()
- pulseIn()
- millis()
- micros()
- delay()
- delayMicroseconds()
In this short tutorial want to control Sugar device with AC Lamp using ATtiny directly as:
- Control AC Lamp ON-OFF
- Control AC dimmer Lamp .
Step 1: Hardware and Software
You need first to download the ATtiny boards folder and install it in arduino hardware path, to do so:
- Download the ATtiny folder.
- Unzip the folder and copy "attiny" folder and move it to your sketch book path "usually for Windows you could find it on "My document" - My document >> Arduino >>Hardware>>attiny -
- An alternative way, if you use arduino 1.6.2 version or newer , go to " tool>> boards>>boards manager and search attiny package and install it .
in both way,after you restart arduino IDE , you can see new board now on tool >> board >> attiny.
For this tutorial i will use ATtiny85 with 1MHz internal oscillator settings
For the hardware part we need:
- ATTINY85-20PU MCU
- Arduino Board as a programmer" I used Arduino UNO"
- Capacitor 10uF
- resistor 10K x 1pcs
- resistor 470R x 1pcs
- LED
- Button x 1pcs
- Potentiometer x 1pcs
- Sugar device.
- RJ12 Cable
- RJ Breakout
- Jumpers
- breadboard
Oscilloscope is optional ,but very important to see the PWM signal and AC Voltage output.
Step 2: Program ATtiny Using ARDUINO IDE
To program ATtiny , first we must prepare arduino Uno to act as a programmer.
- connect arduino Uno.
- Go to arduino IDE , File >> example >> arduino ISP.
- Upload this code to arduino board.
- connect ATtiny to arduino board as in the schematic."Just connect 10uF capacitor between arduino RST pin and GND".
Now we want to set the PWM frequency , in default the PWM frequency is about 62Hz and we will change the frequency to 490Hz to control Sugar device "Sugar device works on PWM frequency from 100Hz - 2KHz"
from ATtiny datasheet we can understand the Waveform generation mode and clock prescaler)(P 79 , 80)
so this code line will modify the frequency of PWM to become 490Hz
TCCR0B = 0<<WGM02 | 1<<CS01
see the video in attachment to see the output PWM frequency
Step 3: First Code : Simple ON - OFF Switch
First this is a simple code to see if your code uploaded successfully.
The LED connected to pin 0 must blink for 1 sec
Blink Code :
<p>/*<br> Blink Turns on an LED on for one second, then off for one second, repeatedly. This example code is in the public domain. */ // Pin D0 on Attiny 85 //Modified by Mohannad Rawashdeh</p><p>//http://genotronex.blogspot.com/ //................................... int led = 0;</p><p>// the setup routine runs once when you press reset: void setup() { // initialize the digital pin as an output. pinMode(led, OUTPUT); }</p><p>// the loop routine runs over and over again forever: void loop() { digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(led, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second }</p>
Now after you check your code works successfully,You can connect the second circuit with Button to turn on the device for 3 Seconds each time you press the button
Simple Switch ON OFF
<p>#include <avr io.h=""> <br>#include <util delay.h=""> int led = 0; int brightness = 12; </util></avr></p><p>// the setup routine runs once when you press reset: void setup() { // declare pin 9 to be an output: pinMode(led, OUTPUT); pinMode(1, INPUT); TCCR0B = 0<<wgm02 |="" 1<<cs01;="" ="" tccr0b="TCCR0B" &="" 0b11111101="" 0x03;="" } <="" p=""></wgm02></p><p>// the loop routine runs over and over again forever: void loop() { boolean pot=digitalRead(1); if(pot) { brightness=64; analogWrite(led, brightness); delay(3000); } else { brightness=12; analogWrite(led, brightness); } delay(100); }</p>
Attachments
Step 4: You Love Sugar Device ? We Need Your Help
Sugar is a startup project , designed and Manufactured the Prototypes in Taipei - Taiwan and Now sugar need your help to become a real product in the market and bring a simple tools to control AC voltage.
Sugar comes in 2 different type:
- Sugar300 : can Handle Up to 300 W
- Sugar1000 : can Handle Up to 1000 W
and both type support AC output voltage control "For Dimmer applications" Hope you support us and become our backer Now on Indiegogo Campaign you can visit our campaign page https://igg.me/at/sugdev/x .
Keep in touch with us on:
Facebook: https://www.facebook.com/SugarWorld.net/
Twitter: https://www.facebook.com/SugarWorld.net/
Instagram: https://www.instagram.com/sugardevice/
Youtube official accounts: https://www.youtube.com/channel/UCDthbcEGR-r0NB49...
Visit Our website to see all features and tutorial and see the story behind sugar
Step 5: AC Dimmer Code
Dimmer code is more useful for many application , here is the schematic for the connection, potentiometer used to control the dimming output.
the oscilloscope waveform show to you the AC output voltage controlled by sugar device.
code:
<p><avr io.h=""><util delay.h="">int led = 0; int brightness = 128; </util></avr></p><p>// the setup routine runs once when you press reset: void setup() { // declare pin 9 to be an output: pinMode(led, OUTPUT); TCCR0B = 0<<WGM02 | 1 <<CS01<wgm02 |="" 1<<cs01;="" ="" tccr0b="TCCR0B" &="" 0b11111101="" 0x03;="" } <="" p=""></wgm02></p><p>// the loop routine runs over and over again forever: void loop() { int pot=analogRead(3); brightness=map(pot,0,1023,12,64); brightness=constrain(brightness,12,64); analogWrite(led, brightness); delay(1000); }</p>