Officers discovered Craig Dimelow, 29, with nearly two kilos of the Class A drug, quantities of adulterant and equipment for cutting the drug.
Lord Woolman said he took into account that Dimelow had acted "under a degree of pressure".
But the judge added: "However, I must take into account you chose to take a significant role in this evil trade. Class A drugs ruin lives. By your acts you facilitated their distribution."
Lord Woolman told Dimelow that he would have jailed him for seven and a half years if he was convicted after trial, but the sentence would be reduced following his guilty plea.
Dimelow, of Leving Place in Livingston, earlier admitted being concerned in the supply of heroin between October 31 and November 21 last year.
The court heard that police mounted a surveillance operation of a flat in the Gilmerton area of Edinburgh where Dimelow regularly attended.
Advocate depute Eric Robertson said "Substantial quantities of Class A drugs, adulterant and paraphernalia for mixing powders" were seized following a search of the flat in Captain's Drive which he frequented.
Dimelow was at the property when police arrived and a face mask, contaminated with heroin, paracetamol and caffeine was found with his DNA on it.
Police earlier watched him leave the property carrying a bulky carrier bag with another man and drive in a van to a car park at a Coop in Dalgety Bay, in Fife, for a drugs drop.
The bag was put in a litter bin with white overalls on top of it. It was recovered by police and found to contain almost £9,000 of heroin.
Police also carried out a search of his home address in Livingston and recovered pounds £10,740 in cash.
He said it was "a relief" for Dimelow when he was arrested. He said the painter and decorator had been taking cocaine but ran up a drug debt.
"He felt that he was pressured into becoming involved by threats," said Mr Renucci.