My HP D5160 printer started its weird behavior by randomly disconnecting the UBS connection to the computer. At first I thought it was a USB hub problem, so I spent some time switching around cables. That didn't work.
Then I suspected a computer problem, but the printer subsequently started flashing warning lights of varied kinds (power, cartridge, etc.), so I replaced the cartridges. It worked OK, but only for a couple of hours and was back to the USB disconnection problem.
I shut it down, disconnected the power and USB and held in the power button for 30 seconds. I reconnected USB and power and it worked for a day. Then the power button wouldn't stop flashing.
I wanted to salvage the printer, as it is used to print on DVDs as well as paper and photo paper. I really didn't want to spend a lot of money on a printer just to print a couple of dozen discs a year, so I purchased a $30 printer just so that I could print on regular paper. I put the D5160 printer on a shelf out of the way.
I just finished a project and couldn't stand the fact that I wouldn't be able to print discs. It looks so much better when you print an attractive disc rather than write on it with a Sharpie. [Insert comment about OCD here ] I decided to try disassembling the printer, as I had nothing to lose by doing so. After removing a couple of parts, I spotted a swollen capacitor on one of the circuit boards! (Remember the swollen capacitor problem with some motherboards a few years ago? https://en.wikipedia.org/wiki/Capacitor_plague)
Great, I found the problem. I was unable, however, to figure out how to disassemble the rest of the printer to get to the circuit board, so back on the shelf it went. Then I thought, "What if somebody asked me how to dissemble their printer"? I'd tell them to Google it.
So I found this: https://www.ifixit.com/Guide/HP+Photosmart+D5160+printer+Disassembly/31410
I followed the directions, removed the board, ordered the capacitor (a better quality one than was originally used), received the capacitor, replaced it, reassembled the printer, and didn't have any leftover parts. I uninstalled the new printer's driver, reinstalled the D5160 driver (I'd removed it before installing the replacement printer), and connected the D5160. It worked!