Electronics & Arduino

Tips, tricks, circuit designs, sketch and code for electronics and Arduino

  • Blog
  • RGB LAMP
  • ACTARUS
  • ArduDome
  • Versione in Italiano

High Power RGB Lamp 1.0 – Arduino (mini pro) controlled RGB Led Lamp with RGB led strips

Posted by Luca Soltoggio on March 19, 2012
Posted in: Arduino projects, Circuit schematics. Tagged: arduino, atmega, led, mini pro, power led, pwm, RGB, sketch. 27 Comments

Finally after a long absence, in which I was immersed in the final realization of this project, I did it!
After months of planning, studies, research and other obstacles, the RGB LED Lamp 1.0 by Toggio is reality.
Here’s the video showing the final result obtained:

And now a brief history.
One of the things that attracted me the most when I first heard of Arduino, was light, and in particular I have always dreamed of achieving an LED lamp RGB.
Nothing difficult, you might think … Except that along this route I found several obstacles.
First of all, I did not find complete projects on the net … There is something, but, in my opinion, with bad algorithms, with little explanation or with complicated methods.
I wanted a simple present to give to the person who I love and that gave me a beautiful daughter …
I wanted something that had a powerful enough light, without too much heat. I wanted something that had a chance to choose the color and not a simple “color cycler.” I wanted something that would serve also as a standard lamp. And finally, I wanted something that was modular and upgradeable in the future.
After these reflections, I began to study the hardware first. I did not want to use the Arduino UNO because it seemed too big for my project. So I decided (and this could open up a huge parentheses) to use a board not so common, but in my opinion awesome (so much that I bought 4 or 5 piece): The Arduino Mini Pro.
(It is understood that in the project of this post, it can easily be replaced by an Arduino UNO or any other board.)
It ‘a very small board (18mm x 33mm), 100% compatible with Arduino Dumilanove, and low power consumption. It is available in four versions: with Atmega328 or with ATmega168, both with 3.3V or 5V.
So I opted in this project for the 5V version with ATmega168. In short it is a very economic solution (it costs about half of the Arduino UNO), very compact and beautiful to see.
I then thought of using the useful LED RGB strips, which offer many advantages over using 1W or 3W power LEDs: they do not heat up, they are powered with 12V and they offer a power up to 13-14W per meter.
Each channel can be powered by a PWM output of the Arduino using a simple transistor (in my case I used the BC337, but for higher power you can use other transistor, or even the MOSFET).
I then proceeded to build a kind of shield for my board. Practically I made a circuit in which the Arduino Pro Mini would fit with strip connectors and with the ability to be removed to upgrade the firmware. Everything has been designed with Fritzing and built by Fritzing Fab.
The final result of the printed circuit is as follows:

You can see the two rows of holes where the Mini Pro will be mounted… Basically, it’s the opposite of a shield 🙂
But let’s step back and see the circuit schematic:

I hope it is clear enough even if I have not drawn just fine!
In any case, the operation principle is as follows: The two 10k potentiometers connected between +5 V and ground, are the two analog inputs that allow to adjust the brightness and other parameters set by the software. The 3-position switch SW2 allows you to select three different functions for the lamp. The digital outputs 3,5,6 go through a resistor to drive the 3 BC337 transistors that will give negative voltage to the 3 channels (R, G, B) of the LED strip. The common positive goes to +12 V, while the Arduino is powered by RAW port (input from 7V to 13V) via a diode that in addition to providing reverse polarity protection, slightly lowers the voltage. SW1 is simply the bridge where will be connected a standard power switch.
Once soldered the components, connectors, potentiometers, mounted the circuit, encapsulated the Arduino board, and assembled it all in a electrician box, the result is as in the following picture:

The size of the box is approximately 10cmx10cmx7cm.
For the top, I used about 70cm of RGB led strip, for a total power of about 10W. To fit the whole strip I rolled it around the support of a “50 CD-R set”. I cut with a utility knife and I glued all with spacers on the box. I then glued with hot glue the wrapping strip led… In short, the job is a little rough but the end result is as follows:

The glass you see on the desk, is the final piece: the glass cover of a IKEA lamp, properly cut ​​by a local glazier, so that it remains empty on both sides. This coverage will finally resting on the box and glued, obtaining the following results:

Not bad right?
Of course there is still the icing on the cake, a not less important part: the software.
The self-explanatory code is as follows:

/*
RGB Led Lamp 1.0a
by Luca Soltoggio
15/03/2012
http://arduinoelettronica.wordpress.com/
*/

int rpin = 3;   // red pin
int gpin = 5;   // green pin
int bpin = 6;   // blue pin
unsigned int r=0, g=0, b=0,bm=0;   // r,g,b, value and blue coefficient
unsigned int valh=0, vals=0, valv=0;   // hue, saturation, value
unsigned int sensorValue;  // analog sensor value

const int analogInPin = A0;  // potentiometer pins
const int analogInPin2 = A1;
const int digitalInPin = 10;  // switch pins
const int digitalInPin2= 11;

int red[]={255,255,135};  // predefined arrays for RGB / White 
int green[]={157,255,158};
int blue[]={51,255,255};

boolean DIG1, DIG2;

long previousMillis = 0;

int i=0,j=0;
int dl=0;  // delay

void setup()                    
{
  pinMode(digitalInPin,INPUT);
  pinMode(digitalInPin2,INPUT);
}
void loop()                    
{ 
  DIG1=digitalRead(digitalInPin);
  DIG2=digitalRead(digitalInPin2);

  if (DIG1) {
    bm=1.1; 
    HSV_Game();
  }
  else if (DIG2) {
    bm=1.9;
    RAINBOW_Game();
  }
  else {
    bm=2.4;
    LIGHT_Game();
  }

  analogWrite(rpin, r/1.09);  // write RED PIN
  analogWrite(gpin, g/1);  // write GREEN PIN
  analogWrite(bpin, b/bm);  // write BLUE PIN
}

// analog input smoothing function
int SensorSmooth (int pin) {
  sensorValue=0;
  for (int i = 0; i<10; i++) {
    sensorValue+= analogRead(pin);
  }
  return int ((float)sensorValue/10+0.5);
}

// first light game: modify HUE and VALUE with potentiometer
void HSV_Game() {
  valh = SensorSmooth(analogInPin);
  vals = 255;
  valv = SensorSmooth(analogInPin2);

  valh = map(valh,0,1023,0,359);
  valv = map(valv,0,1023,32,255);

  hsv2rgb(valh,vals,valv,r,g,b);
}

// second light game: three positions for Warm White, White and Cold White
void LIGHT_Game() {
  valv = SensorSmooth(analogInPin2); 
  valv = map(valv,0,1023,16,255);  
  valh = SensorSmooth(analogInPin);
  if (valh<=281) valh=0;
  if (valh>=742) valh=2;
  if (valh>2) valh=1;
  r=red[valh]*valv/255;
  g=green[valh]*valv/255;
  b=blue[valh]*valv/255;
}

// color cycler
void RAINBOW_Game() {
  dl = SensorSmooth(analogInPin); // delay time
  dl = map(dl,0,1023,1,100);
  valv = SensorSmooth(analogInPin2);
  valv = map(valv,0,1023,64,255);
  unsigned long currentMillis = millis();
  switch (j) {
    case 0:
      r=255*valv/255;
      g=i*valv/255;
      b=0*valv/255;
      break;
    case 1:
      r=(255-i)*valv/255;
      g=255*valv/255;
      b=0*valv/255;
      break;
    case 2:
      r=0*valv/255;
      g=255*valv/255;
      b=i*valv/255;
      break;
    case 3:
      r=0*valv/255;
      g=(255-i)*valv/255;
      b=255*valv/255;
      break;
    case 4:
      r=i*valv/255;
      g=0*valv/255;
      b=255*valv/255;
      break;
    case 5:
      r=255*valv/255;
      g=0*valv/255;
      b=(255-i)*valv/255;
  }      
  if (currentMillis-previousMillis > (long)(100-dl)) {  // use millis instead of delay
    previousMillis=currentMillis;    
    i=i+1;
    if (i==256) {
      i=1;
      j=j+1;
      if (j==6) j=0;
    }
  }
}

/* HSV2RGB function
(c) Elco Jacobs, E-atelier Industrial Design TU/e, July 2011
http://code.google.com/p/shiftpwm/source/browse/trunk/examples/ShiftPWM_Example1/hsv2rgb.cpp?r=3
*/
void hsv2rgb(int hue, int sat, int val, unsigned int& r, unsigned int& g, unsigned int& b) 
{ 
    int H_accent = hue/60;
    int bottom = ((255 - sat) * val)>>8;
    int top = val;
    int rising  = ((top-bottom)  *(hue%60   )  )  /  60  +  bottom;
    int falling = ((top-bottom)  *(60-hue%60)  )  /  60  +  bottom;  

    switch(H_accent) {
        case 0:
                r = top;
                g = rising;
                b = bottom;
        break;

        case 1:
                r = falling;
                g = top;
                b = bottom;
        break;

        case 2:
                r = bottom;
                g = top;
                b = rising;
        break;

        case 3:
                r = bottom;
                g = falling;
                b = top;
        break;

        case 4:
                r = rising;
                g = bottom;
                b = top;
        break;

        case 5:
                r = top;
                g = bottom;
                b = falling;
        break;
    }  
}

The Fritzing project page (where you can also download the source code) can be found here:
http://fritzing.org/projects/high-power-rgb-lamp-10-with-arduino/

Until next time!

ISArduino – Arduino Micro Solar Tracker Robot

Posted by Luca Soltoggio on February 29, 2012
Posted in: Arduino, Arduino projects. Tagged: arduino, breadboard, fritzing, microbots, robot, robotics, sketch. 18 Comments

Here is a simple solar tracker, made with Arduino …
It ‘s just a draft, a starting point to be improved for building new projects.
The idea is very simple. Using two photoresistors (LDRs), one pointing slightly to the right and the other slightly to the left, let’s read the values ​​and move a continuous rotation servo in the direction of the photoresistor receiving more light, until the brightness is about the same on both LDRs.
The end result is what we see in the following video:

As you can see I used Arduino Mini Pro, but nothing change using Arduino UNO. The only difference is that feeding the Arduino Pro Mini I had to use an external regulator, because the inside is not enough to drive the servo.
But let’s get to the list of materials needed:
1 – An Arduino UNO board (or any other compatible)
2 – Two 20K LDR photoresistors (with a few adjustments may also fit other values​​)
3 – Two 4.7K resistors (with different lighting conditions you can use different values)
4 – A continuous rotation servo. (Difficult and/or expensive to find, so I modified a standard servo following the directions found at this link
5 – One 9V battery, a breadboard, plastic holders and little else

The components must be connected to Arduino as follows:

As you see, everything is very simple.
If you want to download the fritzing file, this is the link.
http://fritzing.org/media/fritzing-repo/projects/i/isarduino-micro-robot-inseguitore-solare-con-ardui/fritzing/ISArduino.fzz
Let’s take a look at the following code:

/*
ISArduino 0.1
Arduino Solar Tracker
by Luca Soltoggio - 2012
http://arduinoelettronica.wordpress.com
*/

#include <Servo.h>
#include <Narcoleptic.h>
/*
A library that allows to put the microcontroller in standby
while in idle (delay), saving lot of energy.
You can safely remove it (but in this case you have to replace
the last staement Narcoleptic.delay(15) with delay(15).
*/

Servo myservo;
int Value;
int Center=105;
/*
The variable Center represents the centering value of the servo.
In my case it is 105, while usually it is 90.
With this value, the servo must stop running.
*/

void setup() {
  Serial.begin(9600);
  myservo.attach(9); // Attach servo to PIN 9
}

void loop() {
  int sensorValue = analogRead(A0);  // Read left LDR value
  int sensorValue2 = analogRead(A1); // Read left LDR value

  Value=(sensorValue-sensorValue2)/10;
  /*
  Compute the differnce  between the two sensors
  */

  if (Value==0) myservo.detach(); else myservo.attach(9);
  /*
  Detach servo if idle (for energy saving)
  */

  if (Value>10) Value=10;
  if (Value<-10) Value=-10;
  /*
  Limits the maximum difference between -10 and + 10
  (in order to avoid an excessive speed)
  */

  Serial.println(Value); // Debug
  myservo.write(Center+Value);
  /*
  If "Value" is positive, move the servo to right, else to left
  The speed is directly proportional to the absolute value of "Value"
  */

  Narcoleptic.delay(15);
  /*
  Rather than using a simple delay, we use this to save energy
  You can eventually replace with delay(15)
  */
}

The code is very simple and self-explanatory, so I will not dwell too much.
If you want to download the Arduino sketch, it can be found here:
http://fritzing.org/media/fritzing-repo/projects/i/isarduino-micro-robot-inseguitore-solare-con-ardui/code/inseguitore_solare.ino

Below is the link to the page of the Fritzing project:
http://fritzing.org/projects/isarduino-micro-robot-inseguitore-solare-con-ardui/

Programming the ATtiny85 (or ATTINY45, Attiny84, ATTINY25, ATtiny2313) with Arduino

Posted by Luca Soltoggio on February 19, 2012
Posted in: Arduino, Arduino programming. Tagged: arduino, atmel, attiny, avr, bootloader, communication, IDE, serial, sketch, standalone. 2 Comments

So here you have this long-awaited post.
In this article we will see how to program some of the Atmel ATtiny family, particularly those highlighted in the title, using Arduino libraries and Arduino Uno as ISP. Obviously, everything can be done using a dedicated programmer, but here we will not talk about this subject because the differences are minimal.
Let’s start with a premise: on the ATtiny we will not upload the bootloader, but we will proceed to load our sketch directly into the memory of the microcontroller. So this is a first difference with the ATmega328. There are perhaps some ATtiny bootlaoders, but it doesn’t make sense to load them for several reasons. The first is the small memory of this microcontroller (ATtiny85 has 8Kb compared to 32Kb of the Atmega328). Secondly, most of the ATtiny do not have serial hardware, so it would make not sense to install a bootloader if it doesn’t help to do uploads via serial port.
Let us now see the advantages of the ATtiny85 compared to Atmega328:
1 – lower power consumption (and therefore suitable for projects with battery)
2 – smaller size (the ATmega328 has 28 pins while the ATtiny85 has only 8 pins)
3 – lower cost
Obviously with fewer pins we have a limited number of I/O compared to big brothers.
As regards the programming is very similar to that seen in the previous post and the one before that.
Let’s see the pinout of ATtiny85 that we will use as starting example in this article:

From Arduino point of view:

                             +-/\-+
    Ain0       (D  5)  PB5  1|    |8   VCC
    Ain3       (D  3)  PB3  2|    |7   PB2  (D  2)  INT0  Ain1
    Ain2       (D  4)  PB4  3|    |6   PB1  (D  1)        pwm1
                       GND  4|    |5   PB0  (D  0)        pwm0
                             +----+

So if I want to use the PIN6 as a digital output I will use pinMode (1, output) and if I want to read the analog input from PIN3 I will use analogRead (A2).
Of course, as in Arduino, we can use analog inputs as digital outputs.
The serial hardware does not exist as we said, but we can use the appropriate PIN2 (PB3) as unidirectional software serial. Practically using the standard Arduino command Serial.begin (9600), on PIN2 we have a TX signal that can connect to RX pin of an USB-SERIAL converter, in order to communicate from the ATtiny to the PC, so it’s like having a “read-only serial”.
But let’s get to the point: compared to the programming of the Atmega328, the programming of the ATtiny requires some changes and the adding of some libraries to the IDE.
I recommend using the 0023 version of the IDE, which can be downloaded from the website of Arduino (with other versions it was unsuccessful), and create a separate special folder to be used only ATtiny programming.
So if we already have Arduino (for example in the c:\Arduino folder), we will create a new folder called for example c:\Arduino-Tiny.
After this we have to download the core arduino-tiny-0022-0009.zip, from here, then we have to download the library PinChangeInterrupt-0001.zip from here and finally the library TinyTuner-0003.zip found here. The first file needs to be unzipped in hardware folder (eg: c:\Arduino-Tiny\hardware). It will ask you to overwrite some files. Answer yes without problems. The other two libraries need to be unpacked under the libraries folder (eg c:\Arduino-Tiny\libraries). At this point it should be alright. You can either upload your examples using the following scheme:

Basically, it’s like the ATmega328. The pin 10 need to be connected to RESET, and pins 11, 12, 13 to pin MOSI, MISO, SCK (ie in this case 5, 6, 7), and of course the power supply.
Remember as always to insert the 22uF capacitor between GND and RESET on Arduino UNO otherwise the board will reset and the upload will not work giving you an sync error!!!
At this point, opening our IDE, and an example sketch, we can select Tools -> Boards -> ATtiny85 @ 1Mhz, then File -> Upload to I / O Board.
It will give you an error of type “PAGEL,” but you can safely ignore it. You will then see the message “Done Uploading” that will warn you that everything was successful.
As you can see it is very simple.
The ATtiny by default have fuse set in a way that the microcontroller uses its internal oscillator at 1MHz. And this is the best option for most projects, but if you need more speed, you can program the ATtiny in such a way that it uses its internal oscillator at 8MHz or by using an external oscillator at 16Mhz (see previous posts) . If we want change our ATtiny speed, for example at 8MHz, we need to set the fuse, doing an upload of a empty bootloader, containing only the settings that interest us. To do this, simply select from Tools -> Boards the setting of interest (eg ATtiny85 @ 8MHz), and then Tools -> Burn Bootloader —> w/Arduino as ISP.
At this point you will probably get the following error:

avr_read(): error reading address 0x0000
read operation not supported for memory "lock"
avrdude: failed to read all of lock memory, rc=-2

This error is due to the fact that the is missing the line that concerns the operation memory lock in file c:\Arduino-Tiny\hardware\tools\avr\etc\avrdude.conf the line that concerns the operation memory lock.
So we are going to edit this file and add the following line:

read            = "0 1 0 1  1 0 0 0  0 0 0 0  0 0 0 0",
                  "0 0 0 0  0 0 0 0  o o o o  o o o o";

Under ATtiny85 section, below the line memory “lock” that should look like so after the change:

#   ATtiny85 has Signature Bytes: 0x1E 0x93 0x08.
     memory "signature"
         size            = 3;
         read            = "0  0  1  1   0  0  0  0   0  0  0  x   x  x  x  x",
                           "x  x  x  x   x  x a1 a0   o  o  o  o   o  o  o  o";
       ;
     memory "lock"
         size            = 1;
         write           = "1 0 1 0  1 1 0 0  1 1 1 x  x x x x",
                           "x x x x  x x x x  1 1 i i  i i i i";
         read            = "0 1 0 1  1 0 0 0  0 0 0 0  0 0 0 0",
                           "0 0 0 0  0 0 0 0  o o o o  o o o o";
        min_write_delay = 9000;
        max_write_delay = 9000;
       ;

Now running the bootloader operation should go.
If we want to we can fix also the problem of the message “PAGEL” by adding the following lines:

     pagel            = 0xB3;
     bs2              = 0xB4;

Below the line chip_erase_delay = 4500 in the Attiny85 section. This should look like so after the changes:

#------------------------------------------------------------
# ATtiny85
#------------------------------------------------------------

part
     id            = "t85";
     desc          = "ATtiny85";
     has_debugwire = yes;
     flash_instr   = 0xB4, 0x02, 0x12;
     eeprom_instr  = 0xBB, 0xFF, 0xBB, 0xEE, 0xBB, 0xCC, 0xB2, 0x0D,
                     0xBC, 0x02, 0xB4, 0x02, 0xBA, 0x0D, 0xBB, 0xBC,
                     0x99, 0xE1, 0xBB, 0xAC;
## no STK500 devcode in XML file, use the ATtiny45 one
     stk500_devcode   = 0x14;
##  avr910_devcode   = ?;
##  Try the AT90S2313 devcode:
     avr910_devcode   = 0x20;
     signature        = 0x1e 0x93 0x0b;
     reset            = io;
     chip_erase_delay = 4500;
     pagel            = 0xB3;
     bs2              = 0xB4;

     pgm_enable       = "1 0 1 0  1 1 0 0    0 1 0 1  0 0 1 1",
                        "x x x x  x x x x    x x x x  x x x x";

In this way, the error is corrected but only on Attiny85. To solve the problem for other microcontrollers, here I have given you a starting point. If you do a search on google with the error appearing during the bootloader upload you will find the solution.
There may be an additional error (I can not remember the message) when you upload very large sketchs.
In this case, I remember it was related to a AVR-gcc bug, and in order to resolve it, I needed to download the WinAVR tools, install them, and copy the contents of the folder c:\WinAvrtools in c:\Arduino-Tiny\hardware\tools replacing all files (except previously edited file avrdude.conf).
This last part is a bit more complicated, but you will see that if you will follow everything it will not be difficult.
At the next post then!

Standalone Atmega328 without Arduino bootloader

Posted by Luca Soltoggio on February 10, 2012
Posted in: Arduino, Arduino programming. Tagged: arduino, atmega, atmega328, attiny, bootloader, programmer, sketch, standalone. 5 Comments

In the previous post we saw how to program the Arduino bootloader on a standalone Atmega328, while in another post we saw how to upload the sketch to the microcontroller with Arduino bootloader on board.
In this post I want to highlight what I believe is the ultimate goal of the projects prototyped with Arduino: program the ATmega328 without a bootloader, so that our programs can completely override the flash of the microcontroller.
But why do we do it? The main advantages are the immediate execution of our program when the Atmega power on, and more space for our programs in the memory of the microcontroller. The disadvantage is the fact that it is no longer possible to program via simple serial (less than reload the bootloader), but we must use a dedicated programmer (or a ArduinoISP) and the time to upload the sketch is a lot longer. I advise you to use this mode only on finished project, while using the Arduino bootloader when we are debugging our programs (with continuous uploads).
But we come to practice. The programming is implemented in the same way as the previous post, with a slight modification. We need to edit the usual file “boards.txt”, and add the line xxxxxxx.upload.using = arduino: arduinoisp in the configuration of the board that we are interested in programming, where xxxxxxx is the name of the board. We make a practical example, lets take the configuration to be included in boards.txt we saw the last time for the Atmega at 8MHz: with this new addition would thus become:

##############################################################

atmega328bb.name=ATmega328 on a breadboard (8 MHz internal clock)

atmega328bb.upload.protocol=stk500
atmega328bb.upload.maximum_size=32768
atmega328bb.upload.speed=57600
atmega328bb.upload.using=arduino:arduinoisp

atmega328bb.bootloader.low_fuses=0xE2
atmega328bb.bootloader.high_fuses=0xDA
atmega328bb.bootloader.extended_fuses=0x05
atmega328bb.bootloader.path=arduino:atmega
atmega328bb.bootloader.file=ATmegaBOOT_168_atmega328_pro_8MHz.hex
atmega328bb.bootloader.unlock_bits=0x3F
atmega328bb.bootloader.lock_bits=0x0F

atmega328bb.build.mcu=atmega328p
atmega328bb.build.f_cpu=8000000L
atmega328bb.build.core=arduino:arduino

##############################################################

With this addition, we specify that the upload of the sketch does not have to end up on our Arduino UNO, but on the Atmega we want to program.
Also note the change (optional) atmega328bb.upload.maximum_size whose value becomes 32768. Basically it is the maximum space in bytes available to load our sketch. There is no longer the bootloader so the freed space is available to us.

1 – We upload by the IDE (version 0022) the example sketch ArduinoISP on our Arduino UNO
2 – We turn off everything and then connect a 22uF capacitor between RESET and GND of the Arduino UNO
3 – We Connect pin 10 of Arduino UNO to the RESET pin of the Atmega and pins 11,12,13 to pins 17,18,19 of the microcontroller (or pins 11,12,13 of another Arduino board) as shown in Figure :

4 – We do upload the sketch using the traditional command File -> Upload to I / O board

And that’s it … We programmed the microcontroller to work standalone without Arduino bootloader and without (or almost) external components.
As you can see it is very simple and allows us not only to program the ATmega328 standalone, but also a full Arduino board.
For example I use this system with the Arduino Mini Pro. These are budget boards, which have nearly all the features of the Arduino UNO, but they are very small. Usually, instead of using a standalone Atmega328 within a well-defined project, I use these small boards, and once everything is ready to be canned, I upload the sketch bypassing the bootloader, in particular to make faster ignition.
The Arduino Pro Mini is produced by Sparkfun and is available in 3.3V or 5V and with ATmega168 or ATmega328. I would recommend if the sketch does not exceed 16kB to use the ATmega168 and the 8Mhz version at 3.3V especially if your project is running on battery because it consumes a lot less.
Obviously during programming we have to select the corresponding tab from the list of boards, for example Arduino Pro or Pro Mini (5V, 16 MHz) w / ATmega168.
But the fun does not stop there …
With this system we can also program the ATtiny, the younger brothers of the Atmega. There are several versions but the most interesting are the 8-pin ATtiny85 and the 20-pin ATtiny2313. They have the advantage that consume very little power, are smaller and cheaper.
The ATtiny85 has only 5 I/O, but for many projects is just fine, considering the cost of about € 2.50 in Italy.
Moreover working with internal clock to 1Mhz consumption is really very low, and the size is like that of a NE555.
Can u think how many projects we can simplify using the ATtiny. Even only for example a PWM control of a LED strip, which would usually require operational, capacitors, NE555, and so on, with a simple ATtiny85, a potentiometer, a transistor, a few other components and 2 lines of code, the game is done…
In the next post then!

Loading bootloader on standalone Arduino – Arduino UNO ISP

Posted by Luca Soltoggio on February 7, 2012
Posted in: Arduino, Arduino programming. Tagged: arduino, atmega, atmega328, atmel, avr, bootloader, breadboard, IDE, programmer, serial, sketch, standalone. 14 Comments

In the previous post we saw how to program an Arduino in standalone mode.
One of the requirements was that the bootloader was already preloaded.
But if we had a virgin Atmega328, or if we wanted to program our micro to be used without an external oscillator, what should we do?
If you already have an AVR programmer, it is very simple, and I will not describe here, partly because not everyone has this accessory.
So let’s see how to use our Arduino UNO as ISP programmer.
Is said that with the UNO is not possible but in reality with a little trick, you can bypass the problem.
Do not be discouraged if there are synchronization problems while uploading the bootloader, for every problem there is a solution … but let us see the procedure.
First, I highly recommend you use the 0022 version of Arduino IDE. With version 1.0, I was able to upload occasionally and with difficulty.
I would therefore advise to keep the two installations of the software installed. In un folder the 1.0 and in another the 0022. Both can coexist without giving discomfort with each other.
Then open the 0022 IDE to load the sample file sketch “ArduinoISP“. This example allows you to transform our board in an AVR programmer. Once the file uploaded we can connect our UNO according to the following scheme:

We must then connect as shown, pin 10 of the UNO to RESET pin of the Atmega, and pins 11, 12, 13 to pin 17, 18, 19 of the microcontroller (or pin 11, 12, 13 of another Arduino board ).
In this scheme is not shown, but before we go further we must connect a 22uF capacitor between GND pin and RESET pin in the Arduino board, otherwise during the upload will take place an autoreset and the IDE will give you a sync error.
At this point we can safely select the menu Tools -> Board -> Arduino Duemilanove or Nano w / ATmega328 and launch Burn Bootloader -> w / Arduino as ISP and if everything is done correctly we should have our bootloader loaded on the microcontroller , ready to be programmed via serial as shown in the previous post. Our Arduino UNO however, could be reprogrammed normally (remember to remove the capacitor previously inserted).
Obviously we can not just load the bootloader on an standalone Arduino or on breadboard, but also on another Arduino UNO, 2009, Mini, etc..
But what if you wanted to program the board with minimal configuration without external oscillator?
The procedure is slightly different because in default IDE Boards there isn’t a configuration of this type.
We should manually add the following configuration in the file boards.txt which is located in the “Arduino” folder (under hardware\arduino):

##############################################################

atmega328bb.name=ATmega328 on a breadboard (8 MHz internal clock)

atmega328bb.upload.protocol=stk500
atmega328bb.upload.maximum_size=30720
atmega328bb.upload.speed=57600

atmega328bb.bootloader.low_fuses=0xE2
atmega328bb.bootloader.high_fuses=0xDA
atmega328bb.bootloader.extended_fuses=0x05
atmega328bb.bootloader.path=arduino:atmega
atmega328bb.bootloader.file=ATmegaBOOT_168_atmega328_pro_8MHz.hex
atmega328bb.bootloader.unlock_bits=0x3F
atmega328bb.bootloader.lock_bits=0x0F

atmega328bb.build.mcu=atmega328p
atmega328bb.build.f_cpu=8000000L
atmega328bb.build.core=arduino:arduino

##############################################################

At this point we have a scheme like the following:

Always remember to place 22uF capacitor between GND and RESET, and verify that the sketch ArduinoISP is loaded on our board.
At this point we can load the bootloader by selecting Tools -> Board -> ATmega328 on a breadboard (8 MHz internal clock)
If all goes well, we should have our ATmega328 with Arduino bootloader loaded and running at 8MHz. In this way we can feed our final circuit with 3.3V (if you want), allowing a lower consumption of our project.
If the upload at 16Mhz is successful and that at 8MHz no, it’s because the first upload of 8MHz bootloader needs to be done with the circuit still configured with external crystal, because if we remove the external oscillator, the circuit is not is able to communicate with the ISP programmer. Successively any further loading can be done without the external oscillator circuit.
And here I advance a gem that will be the subject of our next post. If I have finished my project and then I no longer need to use the Arduino bootloader (because I do not need to upload via serial – see previous post), I can upload the sketch directly into Atmega328 memory, overwriting the bootloader. And to do that I have to just use the same connections as seen above …
But for now we stop and we will continue to talk about in the next post.

Arduino Standalone (Arduino without Arduino)

Posted by Luca Soltoggio on February 6, 2012
Posted in: Arduino. Tagged: arduino, atmega, atmega328, bootloader, breadboard, IDE, serial, sketch, standalone, usb. 4 Comments

In this post I will show how to use Arduino in a standalone configuration, ie use the ATmega328 (equipped with a bootloader) in circuit without having to lug around all our Arduino Uno (or the board you have).
The concept is this: I have finished protyping my project with Arduino (such as a RGB Led Lamp) and I’m ready to make the final version of the lamp.
You could you use the Arduino board, but it would be inconvenient not just from the point of view of cost, but also from the point of view of size and consumption … In reality, what we need is just the microcontroller with bootloader and a few external components.
If our Atmega has no bootloader, we need to load it, but first things first … Let us assume that when we already have the bootloader onboard.
To be able to upload the sketch we have to use the connections as shown in the figure:

It important to remove the microcontroller from the board with which we are going to program, otherwise we will program that instead of our Atmega standalone.
As you can see the connections are very simple, you just connect power, TX and RX pins and RESET pin.
At this point from our Arduino IDE we can go to upload the sketch, of course, after selecting “Arduino Duemilanove or Nano w / ATmega328” from the Tools -> Board. And that’s it.
Of course, the same would apply if the Atmega is programmed to work with the 8Mhz internal oscillator. With the difference that we didn’t need the two capacitors and the crystal shown in the figure. To use the internal oscillator, however, we should re-program the bootloader (as we shall see in the next post).
If we have available (and I recommend you to buy it because it is a cheap and really usuful accessory) a USB / Serial converter like the Sparkfun FTDI Basic or Ardino USB-Serial Light, connections would as the following:

It’s nothing complicated, just the connections are almost as before: VCC, GND, RX, TX, while the reset is a bit different because it requires a 100nF capacitor between the pin DTR (or RTS – not to be confused with RST) and Atmega RST.
We have seen how to program a standalone ATmega328 with bootloader preinstalled, in the next post we will see how to load the bootloader on a virgin Atmega328 in both versions: with external 16mhz crystal, and with internal 8Mhz oscillator.

Two simple power LED driver – Part two: Transistor

Posted by Luca Soltoggio on February 5, 2012
Posted in: Circuit schematics. Tagged: arduino, costant current, led, power led, pwm, transistor, TTL. 13 Comments

In the previous post we saw a simple LED driver based on integrated LM317, but as explained one of the faults of the circuit is the high dropout.
Thus we see today a scheme based on a single transistor (in our case a BC337, but any NPN is fine as long as we know all the electronics characteristics), which has a dropout of about 1.25V.
The circuit is as follows:

Of course, you can also use just one LED. I put three in series as example, as we are able to drive three 1W LEDs with a high efficiency. In the abovbe figure, we have a LED power of about 3W, while the whole circuit consumes about 4W.
But let see the operation principle:
The LEDR is a standard LED and serves as a reference voltage for the transistor. The red LED dropout is about 1.8V, then the collector of the transistor will receive approximately 950mV. Always applying Ohm’s law 0.950 / 2,8 = 339, then with R2 at 2.8Ω there is a current leakage of 339mA, which will flow also through the emitter of Q1. In this way we will have a constant current regardless of the input voltage. This is true as long as the voltage will be sufficient to maintain the LEDR at 1.8 V, or 1.2V on the emitter of Q1. So in other words we expect that the input voltage is at least 1.2V higher than the dropout of the LED that we are going to drive. White leds for example, have a maximum voltage of 3.6V, so 3.6 × 3 = 10.8V and 10.8 + 1.2 = 12V. In this case we are perfect. I always recommend a voltage of about 1.5 V more for safety.
If the load to drive is greater than 500-600mA, it is possible to decrease the value of R1 up to 470Ω.
If we want to exceed 750mA instead, we should replace the transistor with a darlington type like the BD677A that can deliver up to 4A!
You can test the circuit by changing the values, using the free simulator online at this link.
Obviously this circuit is “PWM capable.” We can then feed the device via a PWM circuit, or use the following appropriate circuit in order to use a PWM output of Arduino:

In this case we have to slightly decrease the value of resistance R2, due to the fact that the Q1 driving voltage is not 12V but 5V.
The important thing as always is to experiment, experiment, experiment!

Two simple power LED driver – Part one: LM317

Posted by Luca Soltoggio on February 3, 2012
Posted in: Circuit schematics. Tagged: arduino, costant current, led, lm317, logic, ne555, power led, pwm, transistor, TTL. 11 Comments

The use of LEDs in daily life, in recent years has increasingly taken hold.
With the release on the market of power LEDs (1W, 3W, 5W, 10W …) the ability to illuminate our lives with these diodes is within everyone’s reach.
Unfortunately, the integrated power LED (like brand name bulbs) have costs still high enough, but the individual leds are affordable.
The problem for many is the power, as compared to classic 20mA LED  that typically are fed at 12V with a 470Ω series resistor, this type of LED need, considering the power, to be fed not with constant voltage, but with constant current.
When you buy a high power LED, often the seller also provides its specified power driver at prices not always acceptable.
Hence my research on the various sites and blogs for a system to power these LEDs, which give me the ability to be dimmed even by a PWM circuit or by a microcontroller (such as Arduino).
I found two interesting projects, simple, easy to implement, experienced and above all economic and built with components very common and widespread.
The first circuit that I want to present, is constructed using the integrated circuit LM317, which is a variable voltage regulator, but in this case we will use it as a current regulator.

The circuit shown here is quite simple to understand. Between Vout and ADJ is present a reference voltage of 1.25 V, then applying Ohm’s law I = V / R, with a voltage of 1.25 V and a resistance of 3.6 Ω, we will have a constant current of approximately 347mA flowing through LED1, irrespective of the input voltage. This circuit is therefore suitable for example for powering a 1W LED  (which typically uses 350mA).
Of course you can put more LEDs in series, but remember that the dropout is about 3V, so the input voltage must be at least 3V higher than the nominal LED voltage. However, if the voltage is too high the chip will warm up too much as all the excess voltage is dissipated just from LM317.
In fact this circuit from the point of view of yield is not the maximum; to power a 1W LED, the circuit draws approximately 4W.
We must also think about the fact that the power dissipated by resistance is about 1.25V multiplied by the circuit current (347mA in this case), then we should pay attention that resistance is adequate to power (we can put two or more resistences in series or in parallel to avoid this problem).
Everything is “PWM capable“, so we can feed the device with a PWM power driver (maybe i will public one in a future post), or drive the LED with a low power PWM signal, such as the output of a 5V Arduino. In this case the schematic will be modified as follows:

We had to change the value of the resistance R1 because introducing R2, the reference value is brought to 1.75V approx. It should be tested and measured with an current meter.
In this circuit VCC, that is the TTL input of our circuit (PWM output of an Arduino, rather than the logic output of a NE555), when is at a high value (+5V), the transistor Q1 conducts and the ADJ pin of the LM317 goes to ground, inhibiting the flowing of current in R1 and then in the LED. Conversely, if I bring the logic input to a low value (0V) the transistor does not conduct allowing the flowing of current: the LED lights up as in the previous example. Obviously logic works in reverse and then in microcontroller programming this should be taken into account, or at least you can add an additional transistor to invert the logic again.
In a future post, we’ll create another circuit that has the same function, but using a transistor instead of the LM317.

Arduino and ATmega328: A bit of clarity

Posted by Luca Soltoggio on February 2, 2012
Posted in: Arduino. Tagged: arduino, atmega, atmega328, atmel, avr, bootloader, communication, firmware, hardware, IDE, programmer, serial, sketch, software, standalone, usb. Leave a comment

Until a few weeks ago,  I didn’t know exactly what was the difference between Arduino and its “core” Atmega328.
I read here and there that the Atmega328 can work in stand-alone, with or without the Arduino bootloader, and even in its minimal configuration, without any external components.
I was so confused about what exactely is Arduino and the ATmega328 “without Arduino”, as they have not much information on the Internet.
Eventually, with a lot of patience, “munching” here and some information on various blogs, reading magazines and books, and doing field trials, I finally came on the bulb (or LED) in my head and all appeared more clearly.
So I decided to publish this post where I explain briefly and simpy (and probably not in a totally professional way – I would ask apologize if there are any inaccuracies) what is Arduino and what sets it apart from the Atmega328 (which is included in last generation Arduino boards).
If I had to define the Arduino project, i can say that Arduino is an open source hardware & software platform which allows you to use more quickly and easily the potential offered by the microcontroller ATmega328.
The hardware part, is typically represented by the boards listed on the official website, with lots of open schemes that allow us to realize by ourself the various Arduino boards or modify and customize them to our liking. The software part is instead represented by the often quoted bootloader, libraries and by the IDE interface that allows you to program and debug our Arduino board.
But what are the differences between Arduino and its ATmega328 microcontroller and what are the advantages in microcontroller programming?
From the perspective of hardware, the Arduino board (for example Arduino UNO) is ready for use: it has everything we need to program the microcontroller just plugging the board into the USB port of the PC: it has on board an internal power connector, a USB port, a processor that acts as a USB / Serial converter, a power LED, a status LED connected to pin 13, two LEDs indicating TX / RX, female strip header for connection of  I / O ports and of course our ATmega328 microcontroller with Arduino bootloader already loaded.
In this way, opening the IDE and selecting the serial port of the PC we can load our sketch with a button and in a few seconds (typically 2-3). The upload is done via the serial port emulated by USB, which is connected to the TX and RX pin of the Atmega (pins respectively 3 and 2).
From the point of view of the software, the bootloader preloaded on Atmega (whether that is mounted on an Arduino board or just on a breadboard), is what allows serial communication with our PC. Without the bootloader to load the sketch (our “programs”), we would need an AVR programmer, more expensive, a bit ‘more cumbersome to use and slower to load the sketch. The bootloader is therefore a program, a firmware might say, whose job is basically to upload our sketch compiled into the flash memory of the microcontroller via serial communication, making sure that this program is not going to overwrite the memory area where bootloader resides. So when we upload a sketch we’re going to write in a zone of free memory and the bootloader is not affected and remain intact.
The main advantage compared to Atmega without bootloader, as we have seen, is that it can be uploaded via a simple serial communication, and at a faster rate than that which we directly loading the sketch using AVR programmer.
In addition to the bootloader, we have included libraries in the Arduino IDE. These libraries add many functions and constants that make it possible to greatly simplify the code we are going to write making it much more usable than the standard microcontroller. All this is managed through the IDE, which is the Arduino interface in which we write the sketchs, we upload and debug.

That said (hoping not to have bored and to be able explain without creating further confusion) in the next post we will see how to build a stand-alone Arduino, and how to program the ATmega328 without a bootloader, solution that sometimes could be advantageous.

Hello world!!!

Posted by Luca Soltoggio on February 1, 2012
Posted in: Uncategorized. Leave a comment

I’m finally here with this new blog…
I am electronics and Arduino enthusiastic, and I decided to share my projects as well solutions to problems that I have met in this fascinating world.
Shortly i will publish my first project (an Arduino controlled RGB lamp) and some tips on programming Atmega328 and Attiny85 in Standalone mode.

See you soon!

Posts navigation

Newer Entries →
  • Recent Posts

    • Non-blocking “breathing” led
    • Ultra Low Power Led Flasher (with ATtiny)
    • Arduino Yún: welcome future!
    • Simple LDR switch
    • ArduDome: Opensource domotic with Arduino + RaspberryPI + PHP + JSON + jQuery
  • Archives

    • May 2015
    • January 2014
    • October 2013
    • September 2013
    • May 2013
    • April 2013
    • November 2012
    • May 2012
    • April 2012
    • March 2012
    • February 2012
  • Categories

    • Arduino
    • Arduino programming
    • Arduino projects
    • Circuit schematics
    • Uncategorized
  • Tag

    actarus arduino arm atmega atmega328 atmel attiny avr bootloader breadboard communication costant current domotic ethernet firmware fritzing hardware IDE ldr led linux embedded lm317 logic microbots mini pro mosfet ne555 nimh power led programmer pwm raspberry pi RGB robot robotics sensor serial sketch software standalone stratosphere transistor TTL usb wifi
  • Meta

    • Register
    • Log in
    • Entries feed
    • Comments feed
    • WordPress.com
  • Friends

    • Actarus Project
    • My Ip Address
Create a free website or blog at WordPress.com.
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy
  • Follow Following
    • Electronics & Arduino
    • Already have a WordPress.com account? Log in now.
    • Electronics & Arduino
    • Customize
    • Follow Following
    • Sign up
    • Log in
    • Report this content
    • View site in Reader
    • Manage subscriptions
    • Collapse this bar
 

Loading Comments...