Problems with Programming ATTiny85-20PU

ATTiny85-20PU Programming
Background:
I have been using the Attiny85usb clone to produce dcc function decoders for Model railways with some degree of success. (Based on the work here: https://rudysmodelrailway.wordpress.com/ )So the process of using these clones to produce results has been tried, tested and works. I have now attempted to program the ATTiny85-20PuU chip directly using the Arduino ISP. I have managed to successfully program the BLINK program into the chip and it works.
The circuitry consists of an input of 15v ac which originates from the dcc controller which has a dcc signal. The input goes to a common input where it is rectified to 5v dc to power the ATTiny85usb and also through an optocoupler circuit as shown.

I have a circuit board designed to take the ATTiny85 usb and almost all the code I have written for these boards works and I have used them repeatedly. Since the ATTiny85usb board is bulky I have attempted to program the chip directly and I have successfully programmed the BLINK program using the ArduinoISP. I have then taken programs which work on the ATTiny85usb and programmed them into the ATTiny85-20PU.

Problem

  1. Nothing Works!
  2. I in the ATTiny85usb board the optocoupler signal is sent to pin 2(PB2). I have sent the signal to physical pin 7(PB2) on the ATTiny85-20PU. I have tested the connections on the USB board and pin2 is connected to physical pin 7 on the board. Am I correct in doing this?
  3. If I take an ATTiny85-20PU loaded with the BLINK program and connect it to a 5v Power supply fed by the DCC in I get no response (measured 4.9vdc). If I plug in a separate power supply then the led blinks (measured 5.08vdc). What the hell is going on there?

There must be something glaringly obvious which I am missing. I have attached a sample code that works. Any help or advise would be greatly appreciated.

// DCC Function Decoder
// Author: Ruud Boer - October 2015
// This sketch turns an ATTiny85 into a DCC function decoder for F0 - 5
// Output pins used: 3-19 (14-19 = A0-A5). Pin is HIGH when Function is ON.
// The DCC signal is fed to pin 2 (=Interrupt 0).
//  For ATTiny85 USB PinPB0=Pin 0  for ATTiny85-20PU PB0 is Pin 5    For ATTiny85 USB PinPB1=Pin 1  for ATTiny85-20PU PB1 is Pin 6   
//  For ATTiny85 USB PinPB2=Pin 2  for ATTiny85-20PU PB2 is Pin 7    For ATTiny85 USB PinPB3=Pin 3  for ATTiny85-20PU PB3 is Pin 2
//  For ATTiny85 USB PinPB4=Pin 4  for ATTiny85-20PU PB4 is Pin 3    For ATTiny85 USB PinPB5=Pin 5 RESET for ATTiny85-20PU PB1 is Pin 1 RESET
// Optocoupler schematics for conversion of DCC - 5V: www.rudysmodelrailway.wordpress.com/software
// Many thanks to www.mynabay.com for publishing their DCC monitor and -decoder code.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTANT: GOTO lines 15 - 28 to configure some data!
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int decoderAddress = 222; // This is the decoder address, change into the number you want.
//#define F0_pin 1 // Define the output pin for every Function number in use
//#define F0_pin2 3 // 2nd pin for same function is possible. Can use forward / reverse direction ... see line 97.
//#define F1_pin 0 // Available pin numbers: 0,1,3,4,5
//#define F2_pin 0
//#define F3_pin 0
 #define F4_pin 3
//#define F5_pin 0
//#define F6_pin 0
//#define F7_pin 0
//#define F8_pin 0
//#define F9_pin 0
//#define F10_pin 0
//#define F11_pin 0
//#define F12_pin 0

#include <DCC_Decoder.h>
#define kDCC_INTERRUPT 0

byte Func[4]; //0=L4321, 1=8765, 2=CBA9, 3=F20-F13, 4=F28-F21
byte instrByte1;
int Address;
byte forw_rev=1; //0=reverse, 1=forward

boolean RawPacket_Handler(byte pktByteCount, byte* dccPacket) {
  Address=0;
  if (!bitRead(dccPacket[0],7)) { //bit7=0 -> Loc Decoder Short Address
    Address = dccPacket[0];
    instrByte1 = dccPacket[1];
  }
  else if (bitRead(dccPacket[0],6)) { //bit7=1 AND bit6=1 -> Loc Decoder Long Address
    Address = 256 * (dccPacket[0] & B00000111) + dccPacket[1];
    instrByte1 = dccPacket[2];
  }

  if (Address==decoderAddress) {
    byte instructionType = instrByte1>>5;
    switch (instructionType) {
      case 2: // Reverse speed
        forw_rev=0;
        break;
      case 3: // Forward speed
        forw_rev=1;
        break;
      case 4: // Loc Function L-4-3-2-1
        Func[0]=instrByte1&B00011111;
        break;
      case 5: // Loc Function 8-7-6-5
        if (bitRead(instrByte1,4)) {
          Func[1]=instrByte1&B00001111;
        }
        else { // Loc Function 12-11-10-9
          Func[2]=instrByte1&B00001111;
        }
        break;
    }
    // F0 is an example of two output pins that alternate based on loc forw_rev driving direction.
    //if (Func[0]&B00010000) {digitalWrite(F0_pin,forw_rev); digitalWrite(F0_pin2,!forw_rev);} else digitalWrite(F0_pin,HIGH);
   // if (Func[0]&B00000001) digitalWrite(F1_pin,LOW); else digitalWrite(F1_pin,HIGH);
   // if (Func[0]&B00000010) digitalWrite(F2_pin,LOW); else digitalWrite(F2_pin,HIGH);
   // if (Func[0]&B00000100) digitalWrite(F3_pin,LOW); else digitalWrite(F3_pin,HIGH);
    if (Func[0]&B00001000) digitalWrite(F4_pin,LOW); else digitalWrite(F4_pin,HIGH);
    //if (Func[1]&B00000001) digitalWrite(F5_pin,LOW); else digitalWrite(F5_pin,HIGH);
    //if (Func[1]&B00000010) digitalWrite(F6_pin,LOW); else digitalWrite(F6_pin,HIGH);
    //if (Func[1]&B00000100) digitalWrite(F7_pin,LOW); else digitalWrite(F7_pin,HIGH);
    //if (Func[1]&B00001000) digitalWrite(F8_pin,LOW); else digitalWrite(F8_pin,HIGH);
    //if (Func[2]&B00000001) digitalWrite(F9_pin,LOW); else digitalWrite(F9_pin,HIGH);
    //if (Func[2]&B00000010) digitalWrite(F10_pin,LOW); else digitalWrite(F10_pin,HIGH);
    //if (Func[2]&B00000100) digitalWrite(F11_pin,LOW); else digitalWrite(F11_pin,HIGH);
    //if (Func[2]&B00001000) digitalWrite(F12_pin,LOW); else digitalWrite(F12_pin,HIGH);
  }
}

void setup() {
  DCC.SetRawPacketHandler(RawPacket_Handler);
  DCC.SetupMonitor( kDCC_INTERRUPT );
  pinMode(2, INPUT);
  pinMode(0, OUTPUT);
  pinMode(1, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  //pinMode(5, OUTPUT);
}

void loop() {
  DCC.loop();
}

That works where? On the ATTiny85usb board? On the other board? If the latter, wouldn't sharing something that doesn't work there be more useful in debugging?

Showing a schematic of the problematic board would be a good first step.

What do you have the ATTiny85's fuses set to?

Apologies, I thought I made it clear. Everything works when I use the ATTiny85usb board. I have no issues with that. Where it falls apart is the Bare chip. I have not attempted to reset the fuses of the Attiny85 so they are set as they would be ex factory.
What I find baffling is that a Bare chip programmed with the BLINK program works with one 5v supply (external) and does not work with another( rectified circuit from the dcc in.) The boards are not at issue since they work as they should. I can put an ATTiny85 usb to a board and I get the desired result with dcc control.(i.e I can turn leds on and off) I can take the same board and replace the ATTiny85usb with a Bare chip (via Breadboard) programmed with the same code and no response.

And since you haven't shown a schematic as asked, every one else is baffled too. Good luck with your project. I'm done.

Factory default for ATTiny85 is 1MHz internal clock. Maybe your ATTinyusb has been set to 8MHz internal clock and that clock speed is needed for dcc to work. You can change the clock from 1MHz default to 8MHz using the Arduino ISP and selecting "burn boot loader" on the tools menu (I'm using IDE 1.8.13)

1 Like

To me, rectified means AC power turned into a kind of pulsing DC power.

DCC is 12-18V depending on what you got. I would get an adjustable DC-DC Buck Converter to get efficient 5V, or even 7V to run a 7805 regulator for flat 5V. Just make sure to use decoupling capacitors across the chip power and ground with short leads.

Tip, you can wire a long narrow socket like a 2 row PCB to plug the Tiny into and have holes to connect power, resistor, led, ICSP and even a reset button --- then plug the chip in. They make 40-pin narrow factor sockets but a 28-pin socket that a 328P fits in would hold an 8 pin Tiny and have 20 holes to spare that will take and hold leads well.

There is a site, O'Baka Arduino where a 328P is soldered to the chip pins directly so I figure that a socket is less minimal yet more solid than a breadboard!

This is what MIT shows to get ATtiny chips going, maybe a checklist for you?

I have done the programming through an Uno only. It has the USB chip to interface with my RPi4 and supply power.
Yes, and did it on finicky breadboards.

I would bet $1 that you only have a tech detail or two to deal with. That usb part of your tinyusb board does a lot!

Thanks tried that same result

Thanks.. I will have a look at the MIT info. I think you are right about the ATTIny85 usb.

I have successfully developed DCC decoders using the bare ATTINY85 running at 16MHz on internal clock. Programming it on Arduino IDE is very straight forward but you need to remember that if you use the Setup() Loop() mechanism you will lose the use of a timer. If you dont then you'll lose the Arduino debugging support - whatever floats your boat.

Here is a more elaborate circuit diagram for the signal connection:
https://www.youtube.com/watch?v=4Clha9E7iQk

I didn't see any detail about the power supply to the ATTiny - sorry if I missed it - but it is recommended to connect pins 1 and 8 together with a resistor. You should check out the ATTiny25/45/85 datasheet (its over 100 pages but easily skimmed).

Although the DCC signal is two opposing DC signals on the one line I didn't see any mention of how you are rectifying it. In any case there is a good chance that the rectification circuit is creating ripple and that is resetting your ATTiny85 because it is difficult for an electronic circuit to faithfully reproduce a square wave which is what you are dealing with (I'm assuming you are using the DCC signal to provide your power). Even the output from the optocoupler will be skewed because it has a rise and fall time - its explained in the datasheet. If you have access to an oscilloscope you'd be able to see easily if it is the power supply. At the moment I can't think of an easy way to check if your chip is resetting because it would be probably 5k to 8k times a second. That's the best I can do for you. I hope you solve your problem and perhaps post here the outcome.