Attiny85 on Arduino IDE

Hi, wonder if any one can help. Having need to use as small a mcu., as I can in a model rail sketch I chose to use an Attiny85. the trouble is I cannot get the somewhat simple program to work. I have been wondering whether it would be better if I specified the pinouts using 'C'., but I am not sure of my 'C' syntax, vis PORTB = 1; DDRB = 0b00011010; nor do I know how to read analog, etc.
For pins, I am using:
Pin ide location Use
1 reset Rst
2 3 Out
3 4 Out
4 Gnd Grd
5 0 In
6 1 Out
7 A1 in my case. In
8 Vcc ditto 3.6V . Vcc

/******* Multiple Unit H&T Lighting **********
    Title: DMU_Lite
    Author: HCS   Date: 08/01/2016. Amended. 17/01/2016
    Runs off engine DC., power circuit to accommodate
    DCC., running also.
    Microcontroller = Attiny85 or 13A.*/

#include <stdio.h>

boolean flag[2] = {1, 2};     // flags for randome use
boolean frLights = 3;               // front lights high on PIN 2 PB1
boolean bkLights = 4;               // rear  lights high on PIN 3 PB0
boolean dirn = 0;                 // sets motor dirnection as forward on Pin 5. 0 = backwards.
int battery = A1;                 // Charge battery pin used to find charge level Pin 7.
long  charge = 1;                // charge, PB2 supply current to battery if necessary. Pin 6

void setup() {
  pinMode(frLights, OUTPUT);
  pinMode(bkLights, OUTPUT);
  pinMode(battery, INPUT);
  pinMode(charge , OUTPUT);
  pinMode(dirn, INPUT);        // Loco dirnection. Pin 5
  digitalWrite(frLights, HIGH);// Switch on front lights Pin 2
  digitalWrite(bkLights, HIGH); // Switch off rear lights Pin 3
  digitalWrite(charge , HIGH); // Charge battery necessity. Pin 6
  // Serial.begin(9600);       // opens serial port, sets data rate to 9600 bps
}

void loop() {
  /********* Determin dirnection ********/
  boolean dir  = digitalRead(dirn);
  if (dir  = HIGH)    {     // Battery dir'n on pin 5 Attiny85
    flag[1]  = HIGH;
    flag[2]  = LOW;
    digitalWrite(frLights, HIGH); // pin 2
    digitalWrite(bkLights, LOW);  // pin 3
  }
  else    {
    digitalWrite(frLights, LOW);
    digitalWrite(bkLights, HIGH);
    flag[2] = HIGH;
    flag[1] = LOW;
  }

  /******** Battery State *********/
  long val = analogRead(battery);
  if (val <= 3)  {
    digitalWrite(charge, HIGH);
  }
  else if (val > 3.6)    {
    digitalWrite(charge, LOW);
  }
  else                    {
    digitalWrite(charge, LOW);
  }

}

I have also included a sketch of my schema. The cores are those of MR., D.A.Mellis Ver., 1.1.0

Hope you can help.

These should all be byte:
boolean frLights = 3; // front lights high on PIN 2 PB1
boolean bkLights = 4; // rear lights high on PIN 3 PB0
boolean dirn = 0; // sets motor dirnection as forward on Pin 5. 0 = backwards.
int battery = A1; // Charge battery pin used to find charge level Pin 7.
long charge = 1; // charge, PB2 supply current to battery if necessary. Pin 6

Needs ==
if (dir = HIGH) { // Battery dir'n on pin 5 Attiny85

analogRead returns an int, 0 to 1023:
long val = analogRead(battery);

if 3 and 3.6 are supposed to represent volts, you need to do some math here.
Suggest you just go by the int value returned instead.
What is your Vcc level? I can't tell from the schematic. Each bit of analogRead will be Vcc/1023.
Say it 3.3V, to allow for diode drops. Each bit would be 3.3/1023 = 3.225mV
Then a level of 3V would return a reading of 3/.00325 = ~930.
And a reading >= 3.3V would return a reading 1023, or the max reading since the level would be above Vcc.
You don't want the level to exceed Vcc by more than 0.5V to avoid damaging the IO pins.

if (val <= 3) {
digitalWrite(charge, HIGH);
}
else if (val > 3.6) {
digitalWrite(charge, LOW);
}
else {
digitalWrite(charge, LOW);
}

CrossRoads:
These should all be byte:
boolean frLights = 3; // front lights high on PIN 2 PB1
boolean bkLights = 4; // rear lights high on PIN 3 PB0
boolean dirn = 0; // sets motor dirnection as forward on Pin 5. 0 = backwards.
int battery = A1; // Charge battery pin used to find charge level Pin 7.
long charge = 1; // charge, PB2 supply current to battery if necessary. Pin 6

Dear Cross Roads,

Once more thank you for your thoughts, where would we be without you. I made all the alterations and the circuit lighting and direction were found to be working but I had difficulty with a part assembled breadboard circuit proving all was working but this was well nigh impossible without having a fully charged battery and a loco receiving its variable rail DC. So I have decided to obviate a lot of problems and insert a SOT., voltage regulator of Microchips as an MCP1702, 3,3V. The trouble with model rail is that everything has to be of the tiniest size in order to fit.

To build the circuit to provenance, because of the SOT., I will need to make a soldered circuit and prove it all in a working state. I have revised the circuit shema and attach it hereto. I would be pleased if you could cast your eye over it for a minute or two to see whether I have got my head screwed on properley, if you can spare me a moment or two please I would be very much obliged.

Chris S

Battery- is not connected to Arduino Gnd. Needs to be.
Why are pins 6 & 7 connected to battery+? I could see maybe 7 for monitoring the charge, but why 6?
You share 1 resistor with 3 LEDs. Not ideal, but it can work. I think 10 ohm is going to let a lot of current flow, damaging the IO pins.
(3.3V - 1.7V)/10 ohm = 160mA. I'd suggest 56 ohm instead. If the LEDs share the current equally, each will see 10mA. If 1 or 2 blow, the third will get all 30mA and likely fail.

Dera Cross Roads,

Thank you for looking into my project.

The two pins are used as pin6 to receive the level of power being held by the battery on a continuous input and pin7 is used called charge which is committed to high when the battery drops below 3V else the charge line is held low, or that is my intenton.

Please forgive the unconnected 0V line

My calculation for the resistors was or is (3.3V - 1.7V) / 0.15 being a stupid mistake of stating 15mA as 0.15 where it should be 0.015mA. Thanks for pointing out the error. I usually use 6mA for indicator led's but am using 15mA's here because the engine will usually be running in bright artificial lighting and the white lights are headlamps the red being tail lamps and as the red led's are 3mm crystal types running at 3.3V's I believe they didn't need much protection. (?)

I have added a revised drawing which I am going to commit to a perf., board. This will be everything on the drawing excepting the lighting circuit and the polarity switch as these are off the board in the finished article in any case. There terminations will be by typical wire to board screw pcb connectors the feed from the polarity switch being, in reallity, the usual DC., motor feed wires of those leading from the wheel pickups to the motor

I though that in this case where I am using one resistor used as such that led's would be operating in parallel rather than the usual series and so would be able to resist the current under all circumstances. Your advice here would be welcomed.

Chris S

Pin 6 & 7, I still don't follow. Both can only be input, you will blow the pins if try to make them output when connected to the battery. Am I missing something? Pin7 as an ADC input, pin6 as an AIN (analog comparator) input?
If have room, a current limit resistor per LED is preferred vs 3 LEDs in parallel with 1 resistor. If the LEDs are from the same batch/lot they may be fine as is.

CrossRoads:
Pin 6 & 7, I still don't follow. Both can only be input, you will blow the pins if try to make them output when connected to the battery. Am I missing something? Pin7 as an ADC input, pin6 as an AIN (analog comparator) input?
If have room, a current limit resistor per LED is preferred vs 3 LEDs in parallel with 1 resistor. If the LEDs are from the same batch/lot they may be fine as is.

Crikey will I ever get this right........... maybe with your not inconsiderable help.

OK. Let me attempt to state what I am wanting to do with pins 6&7. Firstly Pin 7 is an input where I wish to test the voltage level of the rechargeable cell. Pin 6 is an output, the charge current being delivered to the rechargeable cell in replenishment. All this is to do with the fact that while an electric model train is moving it receives an adjusted voltage in order to provide different speeds over the rails (road in model rail terminology) however whilst static in a station say, then it receives no current and therefore for the lights to remain on as prototypically, they require an auxiliary power source. Hence the battery.

I don't see too much problem in providing each led with it's own resistor and will therefore do that. I was trying to avoid it due to the voltage drop with the resistors in series as I am only playing with some 3.3 volts.

Chris S.

CrossRoads:
Pin 6 & 7, I still don't follow. Both can only be input, you will blow the pins if try to make them output when connected to the battery. Am I missing something? Pin7 as an ADC input, pin6 as an AIN (analog comparator) input?

I have looked at the data sheet and see where you are coming from with your information about comparing and pins 6 & 7 on the Attiny. When I am using them as I am will there be any danger of an internal fault occurring in the chip as I am not using them as a comparitor, please? Latest drawing attached showing protection of the battery pin when charging. May I have already damaged the chip?

Chris S.

You still don't have one resistor per LED. You can't connect LEDs in parallel without individual resistors.

And if I understand correctly, you want to take pin 6 high to directly charge the battery from it when the supply voltage is coming from the regulator?

Also you want to monitor the battery voltage using pin 7, with a diode connected such that current can't flow from battery to the pin?

Edit: And the idea is to power the ATtiny85 with 3.3V from the regulator, then use it's pin 6 to charge a 3.6V battery, which needs about 4.2V to reach full charge?

One other point - the connection from the input of the regulator to pin 5 on the tiny85, labelled "Check direction", is there to monitor when there is a voltage supplying the regulator? (Even though that voltage will exceed the tiny85's supply voltage by the amount dropped across the regulator.)

OldSteve:
You still don't have one resistor per LED. You can't connect LEDs in parallel without individual resistors.I did this to reduce the loss of voltage in the circuit. If I put them in series won't the voltage drop be too much for the 3.3V's to sustain a reasonable voltage

And if I understand correctly, you want to take pin 6 high to directly charge the battery from it when the supply voltage is coming from the regulator?Steve youve got it correctly this is my charge circuit when called upon to do so

Also you want to monitor the battery voltage using pin 7, with a diode connected such that current can't flow from battery to the pin? Correct

Edit: And the idea is to power the ATtiny85 with 3.3V from the regulator, then use it's pin 6 to charge a 3.6V battery, which needs about 4.2V to reach full charge? I was prepared to run the battery at a reduced level. If I cant do this then perhaps I should purchase a new Ni-MH battery. Do you have a suggestion please

One other point - the connection from the input of the regulator to pin 5 on the tiny85, labelled "Check direction", is there to monitor when there is a voltage supplying the regulator? (Even though that voltage will exceed the tiny85's supply voltage by the amount dropped across the regulator.)Got this wrong the wire should have been connected to the incoming Variable pole. New sketch attached. This connection is to check which direction the Locomotive is travelling and then to switch the lighting pins A2 & A3 HIGH or LOW and now controlled through a voltage divider. your thoughts would be welcomed

Latest Code Herewith:
</
/******* Multiple Unit H&T Lighting **********
Title: DMU_Lite
Author: HCS Date: 08/01/2016. Amended. 17/01/2016
Runs off engine DC., power circuit to accommodate
DCC., running also.
Microcontroller = Attiny85 or 13A.*/

#include <stdio.h>

byte flag[2] = {1, 2}; // flags for randome use
byte frLights = 3; // front lights high on PIN 2 PB1
byte bkLights = 4; // rear lights high on PIN 3 PB0
int dirn = 0; // sets motor dirnection as forward on Pin 5. 0 = backwards.
long battery = A1; // Charge battery pin used to find charge level Pin 7.
byte charge = 1; // charge, PB2 supply current to battery if necessary. Pin 6

void setup() {
pinMode(frLights, OUTPUT);
pinMode(bkLights, OUTPUT);
pinMode(battery, INPUT);
pinMode(charge , OUTPUT);
pinMode(dirn, INPUT); // Loco dirnection. Pin 5
digitalWrite(frLights, HIGH);// Switch on front lights Pin 2
digitalWrite(bkLights, LOW); // Switch off rear lights Pin 3
digitalWrite(charge , HIGH); // Charge battery necessity. Pin 6
// Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() {
/********* Determin dirnection /
/analogRead returns an int, 0 to 1023:/
/
* Determin dirnection ********/
byte dir = analogRead(dirn);
if (dir >= 300) { // Battery dir'n on pin 5 Attiny85
digitalWrite(frLights, HIGH); // pin 2
digitalWrite(bkLights, LOW); // pin 3
flag[1] = HIGH;
flag[2] = LOW;
}
else {
digitalWrite(frLights, LOW);
digitalWrite(bkLights, HIGH);
flag[2] = HIGH;
flag[1] = LOW;
}

/******** Battery State ********/
long val = analogRead(battery); /
Voltage read through divider
desired voltage recognition
= 1 volt when Vcc = 3.3V therefore
3/(3.3/1023)=310. therefore rounded
say 310.
*/
if (val <= 930) {
digitalWrite(charge, HIGH);
}
else if (val >= 1023) { // Rechargeable Battery assigned upper voltage 3.3 (Reality 3.6V)
digitalWrite(charge, LOW);
}
else {
digitalWrite(charge, LOW);
}

}
/>
Cheers and thanks,
Chris

Chris, it's really late and I have to hit the sack, but I'll address the two most important issues.

First, you cannot charge the battery directly with an output pin. There's no form of current limiting, so as CrossRoads says, you'll blow the pin. A pin can only provide an absolute maximum 40mA before damage occurs, and a safe level is 30mA or less. I try to stay below about 25mA.

Next, back to those LEDs - you cannot connect two LEDs in parallel. They must always have individual current-limiting resistors. (The forward voltages will never be exactly the same, so the current won't be shared equally, and one LED could easily have to handle most of the current, which will exceed it's rating.) This is just a standard rule with LEDs.

There are other things, but I really must get to bed. It's now almost 1am.

Hopefully someone else can take over where I left off.
Good luck with it.

edit: Just one other thing - after 233 posts on these forums, you should know not to post your code inline. Please use code tags.

OldSteve:
Chris, it's really late and I have to hit the sack, but I'll address the two most important issues.

First, you cannot charge the battery directly with an output pin. There's no form of current limiting, so as CrossRoads says, you'll blow the pin. A pin can only provide an absolute maximum 40mA before damage occurs, and a safe level is 30mA or less. I try to stay below about 25mA.

Next, back to those LEDs - you cannot connect two LEDs in parallel. They must always have individual current-limiting resistors. (The forward voltages will never be exactly the same, so the current won't be shared equally, and one LED could easily have to handle most of the current, which will exceed it's rating.) This is just a standard rule with LEDs.

There are other things, but I really must get to bed. It's now almost 1am.

Hopefully someone else can take over where I left off.

edit: Just one other thing - after 233 posts on these forums, you should know not to post your code inline. Please use code tags.

Thanks for your help,

Could not get the code tags to work try as a might. They were used earlier in this thread.

Sleep well OldSteve. Goodbye.

CrossRoads:
Pin 6 & 7, I still don't follow. Both can only be input, you will blow the pins if try to make them output when connected to the battery. Am I missing something? Pin7 as an ADC input, pin6 as an AIN (analog comparator) input?
If have room, a current limit resistor per LED is preferred vs 3 LEDs in parallel with 1 resistor. If the LEDs are from the same batch/lot they may be fine as is.

CrossRoads,

Can see I have made a hopeless mess of designing this scheme and so I am going to rewrite it using your advice and hopefully the hidden critique hinted at. Thats what becomes of laying off for a while when the stuffs only just got into your brain and to only perhaps a small extent at that.

Cheers, Chris S.

ChrisPSR:
Could not get the code tags to work try as a might.

[code]
your code here
[/code]

will result in

your code here