Showing posts with label Arduino. Show all posts
Showing posts with label Arduino. Show all posts

Sunday, 22 February 2015

DIY-Duino – The minimalist Arduino DIY

I’ve been looking for a simple project to play with since moving states. My goals for a simple project would be:

  • Extend my current knowledge and experience;
  • Useful in terms of my other projects;
  • Something that I haven’t tried before;
  • Uses components that I have on-hand

While hunting around for projects to play with, I found the “DIY-Duino” on Instructables by robonerd (http://www.instructables.com/id/DIY-Arduino-or-The-DIY-Duino/). The DIY Duino is a simplified Arduino clone that works on a single sided PCB and has a minimum of elements ( a couple of resistors, a couple of ceramic and electrolytic capacitors, a 16MHz crystal, a bunch of female pin headers an LED and a pushbutton switch).

DIYDuino

This project was a leap of faith for me because I was using someone else's design where the circuit template was a bitmap image (all of that nasty antialiasing means that the edges aren’t terribly crisp, compared with a raster image).

The first step was to transfer and etch the board. This was done with a reasonable amount of success. My only problems were with the antialiased image tending to make bridges between some of the close solder pads. That’s OK … I was expecting that.

I also had some gaps in some of my traces, that was down to the toner transfer and entirely my fault. I ended up bridging the only significant gap with a piece of cut-off wire (a leg from a previously used resistor … I keep most of my cut-offs for this purpose).

My longer term goal for this board will be to drive an automatic watering system for my veggie patch (I may need to make a few more) and will connect in an SPI (or I2C) network.

After the etch/inspection and correction process, it was simply a matter of populating the board.

Robonerd used jumper wires as bridges on the board (there are three of them) whereas I used 0Ω Resistors. The main reason for this is that I’m not all that keen on the appearance of cut-off jumper wire on the component side of the circuit … plus, the resistors are better insulated. Apart from that, it is an entirely personal choice (AFAIC).

When the board was all populated and soldered up, I got down and did some testing.

The voltage between the input power and the LM780 was 7.89V (battery not at full charge). The voltage between the LM780 and the 2nd capacitor was 4.98V, well within the 5% tolerance for the LM780 and safe for the ATMEGA329P chip.

Next, I did a continuity test throughout the board and found that all of the traces were good and the pin headers were correctly connecting to the DIP28.

Also, I made sure that the LED indicator did was it was supposed to do.

I will still need to test the circuit with the ATMEGA328P in place, but I kinda have to wait until my AVRSIP arrives (I just bought one from adafruit).

Well … that’s it for now. I’ll report later on how the circuit performs as a Duino clone.

Wednesday, 31 December 2014

Plastic Photovoltaic Cell

While I was in the process of moving from Tasmania to Victoria, I payed a visit to one of my friends in Tassie. We were having a relaxing time discussing what we had been doing for the last ten years or so, while I was in Tasmania. My friend suddenly got excited and ran off to the other end of his house and came back with a smallish rectangle of plastic (140mm x 100mm).

I have had to wait a while to play with the cell, but now that I have, I am super impressed.
The cell came from http://www.plasticphotovoltaics.org. Their website says that the Department of Energy Conversion and Storage at the Technical University of Denmark is behind this little gem.
I have tested the cell both indoors (0.67V) and outside in full sunlight (7.56V) and I must say that I am very impressed with the freeOPV. By the way, mine is version 2014 – V2.0 – A1 25.3. The website indicates that I should be able to get around 8V from the 16 strips. I’m getting close to that, so … woot!
Okay, so … what am I going to do with my freeOPV? Well, obviously it has to be something that is in keeping with the renewable energy/green principles of an organic solar cell.
I have a small vegetable patch that is about 3m x 3m sitting next to a 8m x 3m shed. The guttering on the shed feeds in to a 10,000 ltr water tank.. What I would like to do is to make the water usage of the vegetable patch as efficient as I can.
The plan is to make a watering system that responds to the moisture level detected in the soil, turning the water on for a pre-set amount of time when the moisture level falls below a set value.

I will need to embed a hygrometer in the garden that will periodically take a moisture level reading. The Arduino will read the moisture level and compare it to a stored moisture level value. If the moisture level is below the stored value, the pump is turned on for a set amount of time (say 5 minutes). If the moisture level is equal to or above the stored value, the pump remains turned off.
I’ve calculated that the roof of the shed is approximately 24m2. I’ve also calculated the average rainfall (mm) in my area and factored in a water collection efficiency value (0.9).
Area x Average Annual Rainfall x collection co-efficient = Litres Collected Per Annum.
For my system, this means an average annual rainfall of something over 14,000 litres per annum. This gives me a daily water consumption rate of around 38 litres.
Now for some more calculations. Some horticulturalists give an average of 1 inch of water per week or 630 gallons per 1,000 square feet). Damn … conversion time. That’s equivalent to 2384.80942 litres per 304 square meters … roughly 8 litres per square meter, per week.

My garden is 9 square meters, so that would mean that I need to use 70.5 litres per week … or 10.08 litres per day. Right … so I have almost 4 times as much water as my vegetable patch will use.
There are other factors to consider, different vegetable crops require different water levels at different times in their development … plus! If I use water efficient planting (using mulch and increasing the humus in the soil, I need even less water input.
Well, for the sake of this experiment, let’s keep things simple.
If the water level drops down below 10% moisture content, turn the pump on for 5 minutes.
I’m expecting that the pump will not come on all that often and it shouldn’t come on at all on rainy days..
Of course, I will want to be able to log in to the vegetable patch, night or day, from ANYWHERE on the globe and get mission critical information pumped straight to my smart-phone. I want to be interrupted while having a highly charged and pressure lunch with senior business partners with information on the drinking habits of my legumes. Clearly, I will need to embed some Wi-Fi capability into my circuit so that I can interrupt Mr CEO while I water my dinner (more likely I will get a beep while in a job interview).
An Arduino Hygrometer module comes in at around $2.00 on eBay. I’m not sure about the pump control yet, so I can’t comment on cost, but lets say about $20

Monday, 17 November 2014

ATTiny85 – Tutorial 11 – Light Input Controlling Sound Output

This is the final Freetronics 11 – Experimenters Kit tutorial. The other 10 tutorials have been covered and, I’m please to say, I have now completed all 11 tutorials with the ATTiny85.

This tutorial makes use of the Freetronics Light module (a light detecting resistor or LDR) with the Freetronics Sound module (a piezo electric module). The light level is read by the analog LDR and the light level value is passed as a tone to the piezo.

Once again, we are using a web sourced tone function that makes use of the ATTiny85 timers to produce an output tone value. This time, however, we are not using predefined wave cycle values, but using analog light level values to drive the tone.

ATTiny85 Pin - Lesson 11

The Piezo is connected on pin 1 (ATTiny85 pin 6), while the LDR is connected on A2 (ATTiny85 pin 3).

Tutorial11

The LDR also connects VCC to the positive rail and GND to the negative rail.

The modified sketch:

/* Tutorial 11: Light Input Controlling Sound Output */

int piezo = 1;
int lightLevel = 0;
int duration = 300;

void setup()
{
  pinMode(piezo, OUTPUT);
}

void loop()
{
  lightLevel = analogRead(A2);
  TinyTone(lightLevel, 4, duration);
}

void TinyTone(unsigned char divisor, unsigned char octave, unsigned long duration)
{
  TCCR1 = 0x90 | (8-octave); // for 1MHz clock
  OCR1C = divisor-1;         // set the OCR
  delay(duration);
  TCCR1 = 0x90;              // stop the counter

}

I believe that TCCR1 and OCR1C both point to pin 1 (but I’d have to research that a little to make sure). Reason tells me that this is so … reason and I are not always particularly good friends.

It would be nice to reduce the delay so that the tones are more smooth, it would be kinda like a light driven theramin.

Check out the rest of the tutorials here.

Friday, 14 November 2014

AVRDUDE – Pagel and BS2 error

According to Wiki, the Pagel BS2 signals error that happens when you are programming an ATTiny85 (and apparently the 84 too) is a consequence of some missing configuration in the avrdude.conf file that comes with the Arduino.
Apparently, you need to correct the configuration problems and then everything will be fine. It is.
  1. Go to your Arduino IDE installation folder (something like C:\Program Files (x86)\arduino-1.0.4\hardware\tools\avr\etc.
  2. Before you do anything … make a backup of the file avrdude.conf
  3. Open the avrdude.conf file in a text editor
  4. locate the ATTiny85 section in the file
  5. locate the chip_erase_delay = 4500; line
  6. add the following two lines below this:
      pagel = 0xB3;
      bs2 = 0xB4;
  7. locate the memory=”lock” keyword in the ATTiny85 section
  8. replace this section with the following (copy and paste)
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;
;


Do the same for the ATTiny84 section.
You will need to restart the IDE for this to be affected.
After this, you need to
  1. Connect the ATTiny85 to the Arduino using your favourite ICSP
  2. load the sketch into the IDE
  3. Select the ATTiny85 board in the IDE
  4. Select Arduino as ISP in the IDE
  5. Upload your sketch to the ATTiny85.
That should fix the problem.
I have now tested the abovementioned fix and the error message has now gone away. To test this, I loaded the sketch from Tutorial 10, compiled and then uploaded it to the ATTiny85 with the following, successful, result.
image
As you can see, the Pagel and BS2 error messages have gone … YAY! Thank you Wiki Page!
After uploading the sketch to the ATTiny85, I dropped the ATTiny85 chip into the circuit and powered it up, again, success. The circuit does what it’s supposed to do.
All in all, happy camper time.

Tuesday, 4 November 2014

ATTiny85 – Tutorial 6 – Making Things Move With Servos

The Freetronics Tutorial 6 replaces the LED and Resistor connection on pin 11 of the Freetronics 11 with the data connection to a simple servo.

The ATTiny85 version does the same thing … not really much sense reinventing the wheel, huh?

ATTiny85 - Lesson 6 - Making Things Move With Servos

As we learn with Tutorial 7, there are three PWM pins on the ATTiny85 to choose from. I went with the easiest and most convenient … our old friend, pin 0.

If you use the sketch from Tutorial 5 in the Freetronics Tutorial with a servo instead of an LED, then this sketch works admirably.

// Tutorial 6: Making things move with servos
int led = 0;
int brightness = 0;
int delayTime = 10;

void setup()
{
  pinMode(led, OUTPUT);
}

void loop()
{
  while(brightness < 255)
  {
    analogWrite(led, brightness);
    delay(delayTime);
    brightness++;
  }
  while(brightness > 0)
  {
    analogWrite(led, brightness);
    delay(delayTime);
    brightness--;
  }
}
 

Of course, I’m using the ++ and – incrementing function rather than brightness = brightness + 1; and brightness = brightness –1; because I think that it looks better, but that’s just me. Let the spirit guide you in your decision …

Lesson 6 - Running

The green jumper connects from ATTiny85 pin 0 to the yellow connector on the servo, Red connects to Orange on the Servo from the 5V rail and the black jumper connects the brown servo connection to GND.

Here’s a short video of the action.

ATTiny85 Controlling a servo with PWM

Check out the rest of the tutorials here.

Monday, 3 November 2014

ATTiny85 – Tutorial 7 – RGB LED

I was playing around looking at the information at hand on the ATTiny85 PWM and I thought that there were only 2 PWM capable pins on the chip … apparently, I was wrong, there are three. PB0, PB1 and PB2 are all PWM. Until I realised that, I was toying with the idea of software based PWM. There are some pretty good articles, tutorials and pages relating to software PWM, so I’ll probably get around to playing with it, some other time.

In the meantime. I had another look over the Freetronics Tutorial #7 – RGB LED and I decided that the code was a little clunky. The RGB values all jump to their random values and there is a lot of blinking … that’s OK if that’s what you want. Anyway, I had a quick play and decided to make the values rise from 0 to their random value and then back down to 0 so that they fade in and out. It’s still a little inelegant, but for the sake of a decent tutorial, I thought that this would be fairly useful.

ATTiny85 - Lesson 7 - RGB LED

My code is as follows

//Tutorial 7: RGB LED *** EXTENDED

int rPin = 0;
int gPin = 1;
int bPin = 2;

void setup()
{
  pinMode(rPin, OUTPUT);
  pinMode(gPin, OUTPUT);
  pinMode(bPin, OUTPUT);
  analogWrite(bPin, random(0, 255));
  analogWrite(gPin, random(0, 255));
  analogWrite(rPin, random(0, 255));
  delay(500);
}

void loop()
{
  upDown(random(0,255), random(0,255), random(0,255));
  delay(500);
}

void upDown(int r, int g, int b)
{
  //bring the colours up
  int rVal, gVal, bVal = 0;
  for(int rVal = 0; rVal < r; rVal++)
  {
    analogWrite(rPin, rVal);
    delay(10);
  }
  for(int gVal = 0; gVal < g; gVal++)
  {
    analogWrite(gPin, gVal);
    delay(10);
  }
  for(int bVal = 0; bVal < b; bVal++)
  {
    analogWrite(bPin, bVal);
    delay(10);
  } 
 
//  return to 0
  while(rVal>0)
  {
    rVal--;
    analogWrite(rPin, rVal);
  }
 
    while(gVal>0)
  {
    gVal--;
    analogWrite(gPin, gVal);
  }
 
    while(bVal>0)
  {
    bVal--;
    analogWrite(bPin, bVal);
  }
 
}

This is still fairly close to the original, with an upDown function that fades each colour in and then all of them out again.

I used my ATTiny85 ICSP to program the ATTiny85 and the 9V to 5V power regulator to supply the solderless bread board.

Lesson 7 - Board

As you can see, there isn’t anything outside of the Arduino core being used in the code, and the wiring is very straight forward. The layout is as per the Freetronics tutorial (more or less … I’ve added a jumper from the top to bottom rails).

Here is how it looks when it’s running. Bear in mind that the cycle uses a random RGB value and splits it up, so it *should* be different for every cycle.

RGB LED Controlled by PWM on the ATTiny85

Well … it’s now well past my bed-time, so I’m calling it a night.

Have fun ATTiny85ers!

Check out the rest of the tutorials here.

ATTiny85 – Tutorial 5 – Dimming LED using PWM

This tutorial is a very simple conversion from ATMEGA328P to ATTiny85, there is only one pin involved in producing output, so we only need to change from pin 11 to pin 0. On the ATTiny85, there are three hardware PWM pins … pin 0, pin 1 and pin2 , so it’s simply a matter of switching over to Pin 0 and away we go.

ATTiny85 - Lesson 5 - Dimming LED with PWM

I’ve changed the sketch for personal taste (and I think, efficiency), you’re free to use the Freetronics version of the code if you like … it’s no great shakes on something this small.

/* project 5: Controlling LED brightness with PWM */

int led = 0;
int brightness = 0;
int delayTime = 10;

void setup()
{
  pinMode(led, OUTPUT);
}

void loop()
{
  while (brightness < 255)
  {
    analogWrite(led, brightness);
    delay(delayTime);
    brightness++;
  }
  while (brightness > 0)
  {
    analogWrite(led, brightness);
    delay(delayTime);
    brightness--;
  }
}

As you will see from the following image, there isn’t much to the wiring for this project.

Tutorial5

And here’s the circuit running through it’s light/dim wizardry.

Tutorial 5 – ATTiny85 - Dimming LED with PWM

Once again, I’m using my ATTiny85 ICSP to program the ATTiny85 using the Arduino UNO and my 9V to 5V power regulator to supply 5V to the circuit.

Check out the rest of the tutorials here.

Friday, 31 October 2014

ATTiny85 Tutorials

I thought that it may be easier for everyone if I put together a single page that lists all of the ATTiny85 tutorials on this blog so that you can come here and launch off to the tutorial that you want to see.

Once again, these are based on the Freetronics Eleven tutorials that you get when you buy the Experimenters Kit.

My goal is to produce an ATTiny85 equivalent for each of the tutorials in that guide, so that you can take advantage of both the Freetronics basic tutorials and my experimentations with the ATTiny85.

I am a hobbyist, not an expert!

Tutorials

Freetronics Tutorial Comments
01 – Controlling an LED  
02 – Controlling 8 LED 4 LED
03 – Reading Digital (On/Off) Input 4 LED
04 – Reading Analog (Variable) Input  
05 – Dimming LED Using PWM  
06 – Making Things Move With Servos  
07 – RGB LED  
08 – Drive More Outputs With A Shift Register 8 LED using 75HC595
09 – Making Sounds Using alternative tone() function
10 – Detecting Vibrations and Knocks  
11 – Light Input Controlling Sound Output  

I’m going to come back to this article and fill in the blanks as I complete the tutorials, so check in from time to time to see how we get along.

I will include the Arduino sketch along with the article so that you can see how the code differs between the chips. I am still planning on doing the same with the ATTiny84 and I’m likely to use the same format.

Typically, the tutorials will include a pin assignment section, an image or video of the completed circuit, the Arduino code and some commentary on the differences that I’ve encountered and the approach that I’ve taken.

Thursday, 30 October 2014

ATTiny85 Tutorial 8 – Drive More Outputs With A Shift Register

So, I thought that I’d skip ahead a bit and get straight into the control of LED via a shift register. As the ATTiny85 has few pin outs, the main thing to be able to go beyond the simple binary pin to pin scheme is to get a shift register working for you.

For this tutorial, I have tried to fit all of the components onto a half+ board. Of course, I’m using my 5V regulator, so I am cheating slightly. However, the ATTiny85 and the 74HC595 both fit on the board along with the required 8 LED.

The original Arduino sketch includes the instantiation of Serial communication, that hasn’t been enabled on my ATTiny85, so I’m just commenting it out in the sketch. I am also omitting the smoothing capacitor between data and GND, if you want to include it, by all means, knock yourself out.

The shift register tutorial uses only digital pins in the original, so I am substituting like for like in the ATTiny85 platform.

The wiring is a little confusing (probably because I crammed it all into a half+ board), but there really isn’t much to it.

Lesson 08 - Drive More With A Shift Register_bb

So long as you get the connections between the ATTiny85 and the 74HC595, then it’s really just a matter of poke and play (of course, you’ll need to be careful with the Vcc and GND connections!).

ATTiny85 - Connections to Shift Register

I’ve taken the liberty of changing the sketch to something closer to what I’ll actually be using, so beware that there are some functional changes (although very few).

Onto the sketch:

/*
  Shift Register Example
  Turning on the outputs of a 74HC595 using an array.
  Modified for ATTiny85

Hardware:
* 74HC595 shift register
* ATTiny85
* LEDs attached to each of the outputs of the shift register

*/
//Pin connected to ST_CP (12) of 74HC595
int latchPin = 2;
//Pin connected to SH_CP (11) of 74HC595
int clockPin = 3;
////Pin connected to DS (14) of 74HC595
int dataPin = 0;

//holders for information you're going to pass to shifting function
byte data;
byte chaseArray[8];

void setup() {
  //set pins to output because they are addressed in the main loop
  pinMode(latchPin, OUTPUT);
//  Serial.begin(9600);

  chaseArray[0] = 1;   //00000001
  chaseArray[1] = 2;   //00000010
  chaseArray[2] = 4;   //00000100
  chaseArray[3] = 8;   //00001000
  chaseArray[4] = 16;  //00010000
  chaseArray[5] = 32;  //00100000
  chaseArray[6] = 64;  //01000000
  chaseArray[7] = 128; //10000000
 
  //function that blinks all the LEDs
  //gets passed the number of blinks and the pause time
  blinkAll_2Bytes(2, 500);
}

void loop() {

  for (int j = 0; j < 8; j++) {
    //load the light sequence you want from array
    data = chaseArray[j];
    //ground latchPin and hold low for as long as you are transmitting
    digitalWrite(latchPin, 0);
    //move 'em out
    shiftOut(dataPin, clockPin, data);
    //return the latch pin high to signal chip that it
    //no longer needs to listen for information
    digitalWrite(latchPin, 1);
    delay(60);
  }
}

 

// the heart of the program
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
  // This shifts 8 bits out MSB first,
  //on the rising edge of the clock,
  //clock idles low

  //internal function setup
  int i=0;
  int pinState;
  pinMode(myClockPin, OUTPUT);
  pinMode(myDataPin, OUTPUT);

  //clear everything out just in case to
  //prepare shift register for bit shifting
  digitalWrite(myDataPin, 0);
  digitalWrite(myClockPin, 0);

  //for each bit in the byte myDataOut
  //NOTICE THAT WE ARE COUNTING DOWN in our for loop
  //This means that 000001 or "1" will go through such
  //that it will be pin Q0 that lights.
  for (i=7; i>=0; i--)  {
    digitalWrite(myClockPin, 0);

    //if the value passed to myDataOut and a bitmask result
    // true then... so if we are at i=6 and our value is
    // %11010100 it would the code compares it to %01000000
    // and proceeds to set pinState to 1.
    if ( myDataOut & (1<<i) ) {
      pinState= 1;
    }
    else { 
      pinState= 0;
    }

    //Sets the pin to HIGH or LOW depending on pinState
    digitalWrite(myDataPin, pinState);
    //register shifts bits on upstroke of clock pin 
    digitalWrite(myClockPin, 1);
    //zero the data pin after shift to prevent bleed through
    digitalWrite(myDataPin, 0);
  }

  //stop shifting
  digitalWrite(myClockPin, 0);
}


//blinks the whole register based on the number of times you want to
//blink "n" and the pause between them "d"
//starts with a moment of darkness to make sure the first blink
//has its full visual effect.
void blinkAll_2Bytes(int n, int d) {
  digitalWrite(latchPin, 0);
  shiftOut(dataPin, clockPin, 0);
  shiftOut(dataPin, clockPin, 0);
  digitalWrite(latchPin, 1);
  delay(200);
  for (int x = 0; x < n; x++) {
    digitalWrite(latchPin, 0);
    shiftOut(dataPin, clockPin, 255);
    shiftOut(dataPin, clockPin, 255);
    digitalWrite(latchPin, 1);
    delay(d);
    digitalWrite(latchPin, 0);
    shiftOut(dataPin, clockPin, 0);
    shiftOut(dataPin, clockPin, 0);
    digitalWrite(latchPin, 1);
    delay(d);
  }
}

Running

I thoroughly recommend that you do this tutorial on the Arduino first before attempting the ATTiny85 version, so that you know what to expect and how the connections work. Other than that, this makes a nice and tiny board project, now I need to play with laying this out on a board so that I can etch it … that should be fun.

5V Reg - Powered

Testing the ATTiny85 Shift Register Sub Board

The above video is the ATTiny85 and 75HC595 sub board connected to breadboarded LED. This is the next step on from the breadboard version in this article, but uses the same sketch and is functionally identical.

Check out the rest of the tutorials here.

Thursday, 9 October 2014

Testing Arduino Code

There are a couple of ways that you can test your Arduino code, not least of which is compiling your sketch and then uploading it to your microprocessor and making sure it runs OK … however, that doesn’t help in revealing any problems in the code.

If there are major problems then, sure, the IDE will whine and you’ll hear about it. But what if there’s a silent problem in your code that you can’t necessarily find sooner?

I’m currently looking at using a C language IDE (CodeLite) as a testing ground for my code. I haven’t gone too far yet, so there are likely to be some issues (for instance, the byte data type is missing in GCC … apparently, so I’m using the char data type instead).

My thinking is that I should be able to use the C IDE to reveal any problems in my code by sending the data that I want to monitor to stdout and use stepping to inspect the values and variables as it goes along.

Other things to consider … a standard C application starts with int main() rather than void setup() and you have to explicitly define and call the loop() function, oh there are other differences that I’m sure to find along the way, but for simply testing function blocks to make sure that the data that my function is sending (and to a lesser extent, the function structure and flow), can be inspected discretely.

Playing with my Shift Register code, I created the following main.c file

code_grab

and then I ran the code …

cmd_grab

showing that the values being passed are correct.

I can also create a couple of different functions that do the same so that I can then have a look at them from an efficiency, memory and timing perspective to decide on which way is the best for my application.

Anyway, it’s all learning, poking and prodding. I’m not sure how others approach testing their Arduino code … but then, I’m a software tester by profession.

Thursday, 2 October 2014

Arduino ATTiny84 ISP Shield – PCB – Part 2

Here is the completed Arduino UNO ATTiny84 ICSP (ISP) Shield that I have just completed.

ATTiny84 ICSP Shield 02

While soldering the board, I went through and tested each trace with the multimeter so that I wouldn’t have to go back later and find any dud connectivity. It turns out that there weren’t any shorted or broken traces this time, so it may have been a vein effort, however, I know that when I plug it in and run the circuit, that at least there aren’t any hardware failures.

The Arduino IDE test (using the UNO as an ISP) went without a hitch too, so it seems that all of my errors were caught early on and I ran out of problems … yay!

I am quite pleased with the two ICSP, they work well, have minimum components and an elegant design.

To recap on some of the design elements, below is the connections from the ATTiny84 to the Arduino UNO.

ATTiny84_To_Arduino

You can see that the connections for the ATTiny84 are essentially the same as  theATTiny85, just arranged differently.

I have been thinking about using a tri-coloured LED (common cathode) to replace the three 3mm Red, Green and Blue LED … but that can wait for now, I really don’t need the extra ICSP.

Arduino as ISP for ATTiny84

Following on in the same vein as my previous articles on Arduino as ISP for ATTiny85 IC, this article addresses the ATTin84. I refer you to the previous posts in the thread ATTiny85 thread for more information on plans, designs and etcetera, particularly Arduino as ISP for ATTiny85.

As I have mentioned in previous articles, there are very few differences between the 84 and the 85 in terms of pinouts. The arrangement and number of the pins are probably the most significant differences. The components required are virtually the same (you need a 14 pin DIP rather than an 8 pin … and obviously, you need an ATTiny84 IC).

ATTiny85_To_Arduino

Above is the pinout for the ATTiny85 IC and how it connects to the Arduino UNO. Below is the pinout for the ATTiny84 and how it connects to the Arduino UNO.

ATTiny84_To_Arduino

The differences between the two chips mean that there is some significant difference in the arrangement of the PCB that I designed for the ATTiny85 but the footprint is nearly identical and the ArduinoISP sketch that comes with the Arduino IDE needs no modification at all.

The Blink sketch also does not need modification from the already modified ATTiny85 version of the sketch, however, instead of connecting the LED to pin 5 (PB0), we are now connecting to pin 2 (PB0). Well … I still need to verify that once I’ve finished building the ISP and then breadboarded the ATTiny84 IC in the same test configuration as the previous article. But from my current reading, that should be just Jim Dandy.

Wednesday, 24 September 2014

Arduino – One Button Multiple Functions

One of the things that I want to do with my LED functions is to control them with a single momentary switch. This means that I’ll have less hardware that the user needs to interact with, but the trade-off is that the user will need to press the button multiple times to get the function that they want.

The idea is to use a momentary switch that supplies current to the three LED functions (Glow, Chase, Lamp). I plan to do this by implementing an ATTiny85 to serve as a switch controller. First, though, the proof of concept is by controlling three LED with one momentary switch.

The breadboard is fairly straight forward. The Momentary Switch is connected to Pin 12 on the UNO and has a pull-down resistor. The three LED (acting as proxies for the ATTiny85 that will control the LED functions) connect to Pins 7, 8 and 9 on the UNO. When the button is pressed, a variable is incremented and passed to a switch statement, so that you get all possible combinations of the three LED (X00, 0X0, 00X, XX0, 0XX, X0X, XXX and 000) (I think).

One Button Three Functions

I’ve had a bit of a look around on the interweb and haven’t found anything startling … so I’ve used the basic Button example on the Arduino site and mixed it up with the switch control to arrive at a design that I think will work.

Most of the smarts for this happen in the sketch.

int buttonPin = 12;

int fi = 7;
int se = 8;
int th = 9;

int buttonPushCounter = 0;  
int buttonState = 0;        
int lastButtonState = 0;    

void setup()
{
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
  pinMode(fi, OUTPUT);
  pinMode(se, OUTPUT);
  pinMode(th, OUTPUT);
 
  digitalWrite(fi, HIGH);
  digitalWrite(se, HIGH);
  digitalWrite(th, HIGH);
  delay(300);
  digitalWrite(fi, LOW);
  digitalWrite(se, LOW);
  digitalWrite(th, LOW);
}

void loop()
{
// read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++;
      if(buttonPushCounter == 9){ buttonPushCounter = 1;}
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    }
    else {
      // if the current state is LOW then the button
      // went from on to off:
      Serial.println("off");
    }
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;
  switch(buttonPushCounter)
  {
    case 1:
      digitalWrite(fi, HIGH);
      digitalWrite(se, LOW);
      digitalWrite(th, LOW);
      break;
    case 2:
      digitalWrite(fi, LOW);
      digitalWrite(se, HIGH);
      digitalWrite(th, LOW);
      break;
    case 3:
      digitalWrite(fi, LOW);
      digitalWrite(se, LOW);
      digitalWrite(th, HIGH);
      break;
    case 4:
      digitalWrite(fi, HIGH);
      digitalWrite(se, HIGH);
      digitalWrite(th, LOW);
      break;
    case 5:
      digitalWrite(fi, HIGH);
      digitalWrite(se, LOW);
      digitalWrite(th, HIGH);
      break;
    case 6:
      digitalWrite(fi, LOW);
      digitalWrite(se, HIGH);
      digitalWrite(th, HIGH);
      break;
    case 7:
      digitalWrite(fi, HIGH);
      digitalWrite(se, HIGH);
      digitalWrite(th, HIGH);
      break;
    case 8:
      digitalWrite(fi, LOW);
      digitalWrite(se, LOW);
      digitalWrite(th, LOW);
      break;
  }
  Serial.println(buttonState);
}

I think that I can probably simplify the code a bit more, but it works OK. The only real problem that I have with this code is that the button press has to be fairly confident … a little press does nothing.

I guess that you could scale the number of functions up to the number of pins that you have available, or maybe a shift register? Dunno, I haven’t tried that yet.

The next step with this design is to replace the 3 LED with 3 ATTiny85 to see if that works. The plan is to connect Vcc on the ATTiny85 to Pins 7, 8, 9 of the UNO, if it works the way I hope, then the button press will turn each of the ATTiny85 on and off according to the switch statement. We’ll see.

Saturday, 20 September 2014

ATTiny85 – Chasing LED sketch – Revised

I wanted to make the chasing LED sketch more efficient so that I could reuse the code for chained Shift Register (74HC595) chips without having to touch the code much.

There is really only one change that needs to be made programmatically to do this (I think). I figured that, if I set the number of LED in a variable in the header of the sketch, that this would be the only place that needs to be updated.

Of course, to prove this, I’m going to need to get another 74HC595 chip and connect it up (with the additional LED and resistors. So, at the moment, this is really only theoretical.

In addition to setting the number of LED in a variable, I’ve changed the “for” loop to a “while” loop, that’s really just preference, but you’ll see what it does in the code.

/*
  Shift Register Chasing LED
  Sends the data value to the shift register to
turn each
  LED on in turn for a chasing lights circuit
*/
int latchPin = 2;  // connected to ST_CP (12) of 74HC595
int clockPin = 3;  // connected to SH_CP (11) of 74HC595
int dataPin = 0;   // connected to DS (14) of 74HC595

int nLED = 8;      // 8 LED connected to the array
int nPause = 60;   // the delay between each cycle

byte data;

void setup()
{
  pinMode(latchPin, OUTPUT);
  blinkAll_2Bytes(2, 500);
}

void loop()
{
  int i = 0;
  byte ledValue = 1;
  while( i < (nLED))
  {
    digitalWrite(latchPin, 0);
    shiftOut(dataPin, clockPin, ledValue);
    digitalWrite(latchPin, 1);
    delay(nPause);
    ledValue = ledValue * 2;
    i++;
  }
}

void shiftOut(int myDataPin, int myClockPin, byte myDataOut)
{
  int i = 0;
  int pinState;
  pinMode(myClockPin, OUTPUT);
  pinMode(myDataPin, OUTPUT);
  digitalWrite(myDataPin, 0);
  digitalWrite(myClockPin, 0);
 
  for(i = nLED-1; i>=0; i--)
  {
    digitalWrite(myClockPin, 0);
    if (myDataOut & (1<<i))
    {
      pinState = 1;
    }
    else
    {
      pinState = 0;
    }
    digitalWrite(myDataPin, pinState);
    digitalWrite(myClockPin, 1);
    digitalWrite(myDataPin, 0);
  }
  digitalWrite(myClockPin, 0);
}

void blinkAll_2Bytes(int n, int d) {
  digitalWrite(latchPin, 0);
  shiftOut(dataPin, clockPin, 0);
  shiftOut(dataPin, clockPin, 0);
  digitalWrite(latchPin, 1);
  delay(200);
  for (int x = 0; x < n; x++) {
    digitalWrite(latchPin, 0);
    shiftOut(dataPin, clockPin, 255);
    shiftOut(dataPin, clockPin, 255);
    digitalWrite(latchPin, 1);
    delay(d);
    digitalWrite(latchPin, 0);
    shiftOut(dataPin, clockPin, 0);
    shiftOut(dataPin, clockPin, 0);
    digitalWrite(latchPin, 1);
    delay(d);
  }
}

The “while” loop starts with a ledValue of 1 and, for each loop, it multiplies ledValue by 2 to get the next lamp ID (1, 2, 4, 8, 16, 32, 64, 128) and it increments “i” stopping when i = nLED. The ledValue is a byte value and this is sent to the shiftOut function.

Secondly, the “for” loop in the shiftOut function also takes the number of LED (nLED) as a control value to set the pin state.

The other change that I’ve made to the sketch is to set the delay value as a variable in the sketch header so that I can change that value quickly, without having to hunt for it in the code.

Anyway, I’ll come back to this sketch when I’ve put in the second shift register (not until next week, I’m afraid … I’m off on holidays right now and several hundred kilometres away from my equipment and components).

Friday, 19 September 2014

ATTiny85 Tutorial 3 – Reading Digital (On/Off) Input

I’m still trying to find the time to do these tutorial projects in amongst the rest of the things that I’m doing … so here goes with Tutorial 3 – Reading Digital (On/Off) Input for the ATTiny85.

The main thing to note here is that I’m only using 4 LED rather than the 8 LED that you will find in the Freetronics tutorials. Of course, that’s because the ATTiny85 doesn’t have the masses of pins that the UNO does, so I’ve scaled it back to 4 LED and modified the sketch accordingly.

The other thing to consider is that the available pins for the ATTiny85 are enumerated differently, so, rather than having pins 6 through to 13, we have pins 0 – 4. I need to use one of those pins as a digital input, so that leaves me with 0 – 3.

ATTiny85 Pin assignment

I’ve marked out the pins with the associated sketch variable so that you can see at a glance what connects where.

And here’s the sketch that I loaded onto the ATTiny85 (using my handy-dandy ATTiny85 ICSP from my previous article).

int ledCount = 4;
int ledPins[] = {0,1,2,3};
int ledDelay = 300;
int buttonPin = 4;

void setup() {
  for(int thisLed = 0; thisLed < ledCount; thisLed++) {
    pinMode(ledPins[thisLed], OUTPUT);
  }
  pinMode(buttonPin, INPUT);
}

void loop() {
  for(int thisLed = 0; thisLed < ledCount; thisLed++) {
    digitalWrite(ledPins[thisLed], HIGH);
    delay(ledDelay);
    while(digitalRead(buttonPin) == HIGH) {
      delay(10);
    }
    digitalWrite(ledPins[thisLed], LOW);
  }
}

The sketch initialises the ledCount variable, the ledPins integer array, the ledDelay and the buttonPin variable. The setup function sets the led pins as output and the button pin as input.

The loop cycles through the array making each LED turn on and then turn off, when the LED is HIGH, if the momentary button is held down, the LED stays HIGH until the momentary button is released, then it just keeps going through the cycle.

Project3

As you can see from the above image, the wiring for this circuit is also pretty straight forward. You will note in the top right hand corner, I’m using my 9V to 5V power regulator mini-board. This is another circuit that I completed in a previous post.

This is a very simple tutorial and it does not require any hard to find parts. Instead of using my regulator, I could have connected the positive rail to the 5V of the Arduino and the negative rail to the GND of the Arduino and then slaved the power from the Arduino via a USB connection to my computer, that’s OK and it’s the easiest way to do it if you don’t already have a 5V regulated power supply. Also, you could have breadboarded the ICSP rather than using a dedicated circuit … but since I have them and I built them for this purpose … I’m going to use them!

Well … there you go, Tutorial #3 – Reading Digital (On/Off) Input converted for the ATTiny85 for your entertainment and my fun.

Check out the rest of the tutorials here.

ATTiny85 ISP For Transfer Printing

Here are the toner transfer images for the Arduino to ATTiny85 ISP.

These images are unscaled so, when you print them, make sure that you print them without scaling (make sure your printer isn’t going to make the image “fit” the page.

The mirrored copper bottom of the board.

Fritzing ATTiny85 ICSP_etch_copper_bottom_mirror

and the silk top of the board.

Fritzing ATTiny85 ICSP_etch_silk_top

I’ve printed from the PDF output from Fritzing, so I cannot guarantee that these will work properly. I would recommend that you print the copper bottom out and then see if it matches up with the pin outs of the Arduino (or clone) that you are using.

These images relate to the Arduino ATTiny85 ISP Shield PCB article.

Tuesday, 16 September 2014

ATTiny85 Tutorial 2 – Controlling 5 LED

This tutorial expands on the simple blinking LED by adding more LED to the board. One of the challenges with using the ATTiny85 is that there are far fewer pins that you can use to do stuff. This is, of course, also an advantage as it means that the ATTiny85 is much smaller and, therefore, more practical in small spaces. You have a greatly reduced footprint to shove into small appliances.

To keep to the spirit of the Freetronics tutorial, I have reduced the number of LED from 8 to 5, as that is the maximum number of pins that can be used without reassigning the RESET (PCINT5) pin. The RESET pin is able to be reassigned, however, changing it back to RESET requires high voltage programming and I’m not going into that here.

So, the connections and layout part.

Lesson 02 - Controlling 5 LED_bb

The pin scheme is fairly straight forward

  • LED 1 connects to PIN 5
  • LED 2 connects to PIN 6
  • LED 3 connects to PIN 7
  • LED 4 connects to PIN 2
  • LED 5 connects to PIN 3

The cathode of each LED connects to a resistor, each resistor connects to the ground rail.

The sketch is also quite straight forward. Instead of assigning the pin ID to a static variable, we are assigning it to an array and then iterating through the array step by step turning the LED HIGH and then, after the defined delay, back to LOW.

/*
  Control 5 LED
*/
int ledCount = 5;
int ledPins[] = {0, 1, 2, 3, 4};
int ledDelay = 125;

// the setup routine runs once when you press reset:
void setup() {               
for (int thisLed = 0; thisLed < ledCount; thisLed++)
  {
    pinMode(ledPins[thisLed], OUTPUT);    
  }
}

void loop() {
  for (int thisLed = 0; thisLed < ledCount; thisLed++)
  {
    digitalWrite(ledPins[thisLed], HIGH);
    delay(ledDelay);
    digitalWrite(ledPins[thisLed], LOW);
  }
  for (int thisLed = ledCount; thisLed > 0; thisLed--)
  {
    digitalWrite(ledPins[thisLed], HIGH);
    delay(ledDelay);
    digitalWrite(ledPins[thisLed], LOW);
  }
}

There were some errors in the Freetronics sketch that meant that copy and pasting was not practical. There was a trailing “\” character and the loop variable for the ledCount contained a typo. The above code works and has been tested … feel free to copy/paste it if you want … or just type it out, it isn’t long.

The code will result in each LED illuminating in turn for 125 milliseconds in a kind of scanning LED fashion.

Check out the rest of the tutorials here.

Monday, 15 September 2014

ATTiny85 Tutorial 1 – Control an LED

The first tutorial that pretty much everyone covers is the “Hello World” tutorial of physical programming … connecting a single LED to the microprocessor and causing it to blink.

This is a very simple project and the outlook is sunny.

For this tutorial, you will need to prepare your ATTiny85 with the Blink sketch that comes with the Arduino IDE. This sketch uses the Arduino Pin 13 as the output pin, however, the ATTiny85 doesn’t have a Pin 13. To hook up our ATTiny85, we’re going to need to work out which pin would be compatible with the Arduino Pin 13. So … what are the characteristics of Arduino Pin 13? Pin 13 kinda special, apart from being a Digital interface, it is also an SPI pin (MOSI – Master Out Slave In) for asynchronous communication … typically used for data connection, like when you are connecting your ATTiny85 to the ATMegaxxxx for programming. For this tutorial, you don’t need to access that functionality, but we are looking for a Digital pin on the ATTiny85

ATTiny85_Pinout

The above shows the pinout scheme for the ATTinyx5. We could use PCINT0, PCINT1, PCINT3, PCINT4 or PCINT5 (or … any pin that isn’t VCC or GND). For simplicity sake, we’ll use PCINT0.

Having decided on which pin we are going to use as output in our experiment the next step is to change the example sketch.

Open the example sketch File > Examples > 01. Basics > Blink in the Arduino IDE

IDE_LoadBlink

The sketch that is loaded is the basic blink sketch

The only change that needs to be made to the example sketch is the pin variable

So you change:

int led = 13;

to

int led = 0;

And then you can upload the sketch to the ATTiny85 (remembering to first set up the Arduino as an ISP as per the previous article (Arduino as ISP for ATTiny85).

When the sketch is loaded, you can pop the chip out of the ISP shield and put it in your breadboard as per the following diagram.

Lesson 1 - Controlling an LED_bb

By convention, I’m using the Fritzing example 5V regulator (with the added 9V battery) to show the input source. Practically, I use my 5V regulator board from my previous article in it’s place.

The ATTiny85 is oriented with the dot bottom left, make sure that you orient your chip and connections correctly.

A jumper wire connects PCINT0 to the LED anode and the LED cathode is connected to the GND rail via a resistor (I’m using a 220 ohm resistor, but you should use the correct resistor for your LED).

When the power is connected, the LED blinks.

That concludes the first tutorial. I recommend that you also read the Freetronics tutorial so that you can compare the ATTiny85 implementation with the UNO clone implementation.

Check out the rest of the tutorials here.

Sunday, 14 September 2014

Freetronics Eleven Tutorial to ATTiny85

Right-o … now that I have my Arduino to ATTiny85 ICSP working, it’s time to start working out the practical differences between the Arduino and the ATTiny85.

When I bought my Freetronics Experimenters Kit from Jaycar, the kit came with an Experimenters Guide (Experimenters Kit - Getting Started Guide). The guide, whilst basic, was very helpful and was quite good. The guide is designed to give the experimenter some confidence in the platform and their own ability, so … job done.

What I plan to do is to work through the 11 tutorials in the guide and apply them to the ATTiny85. Some tutorials have to be modified right from the get-go because of the simple fact that there aren’t as many pins on the ATTiny85 as there is on a UNO or UNO clone board. The one that I’m thinking of here is “Project 2: Controlling 8 LEDs”. Sure, you can control 8 LED with an ATTiny85, but the tutorial is about handling each LED from a single pin. This could be done using a shift register or charlieplexing, but that isn’t in the spirit of the tutorial, so instead this will be “Controlling 6 LED”.

I won’t be going over the ICSP and uploading sketches to the ATTiny85, that was pretty much covered in the Arduino ATTiny85 ISP articles.

I will preface each of the tutorials with the pertinent information from the original tutorial and show the converted breadboard layout and sketch from the perspective of the programmed ATTiny85.

Because of the absence of the UNO in the tutorial, it is expected that the breadboard will take it’s power source from a 5V regulated power supply. I recommend that, if you want to build these tutorials, that you also build that circuit (9V to 5V voltage regulator).

Basic with 5V Reg_bb

The first Tutorial “01 - Controlling an LED” is the simple blink sketch and really doesn’t need much explanation, however, I will be starting with this tutorial as a basic building block tutorial and it may prove useful to some.

I am also considering doing the same thing with an ATTiny84 so that there will be a source of these tutorials on the Interweb where people who are interested in starting out with the 84 and 85 can come and see the tutorials from the perspective of the platform that they are using.

Anyway … on with the show.

Saturday, 13 September 2014

Arduino ATTiny85 ISP Shield – PCB – Part 3

Alrighty, following on from my previous post (Arduino ATTiny85 ISP Shield - Part 2), I’ve discovered that the trace on my board for PB1 to Pin 11 was faulty (the toner transfer wasn’t all that good). This resulted in the Arduino reporting an incompatibility error when using the Arduino ISP sketch to upload a sketch to the ATTiny85.

The solution was to “draw” solder along the trace to fix the continuity.

To do that, the copper is heated with the soldering iron and the flux makes the solder to stick to the trace. I call it “drawing” because you are kinda drawing solder on with the iron. Where the copper has pitted too much, the flux doesn’t bond to the copper very well, so I had to go over the trace a couple of times to get a reasonably thick solder line.

I went over the trace a couple of times and built up a continuous line of copper across the break in the line and tested the continuity a couple of times until I got a good signal from the pin to the socket.

The next thing that I did was that I went over the entire board and patched bit’s that looked a little dodgy and where the solder fillets were not quite good enough.

Finally, I plugged in an ATTiny85 chip, connected the shield to the UNO (this time I used the Freetronics Eleven clone), connected the UNO to my PC and launched the Arduino IDE.

First, I loaded the Arduino ISP sketch to the UNO and then I changed the board to ATTiny85 (1MHz clock) and the programmer to the Arduino ISP setting and uploaded the blink sketch to the ATTiny85.

The LED all lit up at the correct time and the IDE reported the usual avrdude miss-error … I am now a happy camper. The design worked, the production of the board worked and finally, the upload of the sketch to the ATTiny85 worked too … woo-frickin-hoo!

Paypal Donations

Donations to help me to keep up the lunacy are greatly appreciated, but NOT mandatory.