Prosecuting, Philip Standfest told Leeds Crown Court that police officers came to Bartlam's address in Latchmere View, West Park with a drugs warrant to search the house.
Officers said that when they entered the premises they saw Bartlam "running round in a state of panic". He was told to stand still, which he ignored, before police became aware of smoke, caused by Bartlam stamping on his mobile phone.
Police then found 40 cannabis plants growing in the loft, the street value of which was estimated to be around £22,000.
The court heard that Bartlam, 32, had a £2,500 debt with his dealer, and was given the choice to either grow the plants, or to have debt collectors sent to his house.
He was doing it to pay off a debt - to meed the debt caused by his own drug habit."
The court heard from a probation officer that he Bartlam was the sole carer for his nine-year-old son, and that he had reduced his cannabis habit to a £20 bag "every one or two days". It was also heard that Bartlam was in receipt of Universal Credit, but had begun a startup business in digital advertising and gets some financial help from his parents.
Pointing out that this cannabis habit works out at between £300 and £600 a month, Recorder Simon Myerson QC said: "How do we assess this reality of the care he gives his child when he spends 95 per cent of his income on cannabis?
"This is a man who is spending 95 per cent of his lawfully acquired money on his drug habit and sponges off his parents.
"I can't reconcile his staggering cannabis use to the benefits he gets.
"If he is paying off a debt, and mitigates his own expenses by using it, and is producing commercial quantities of cannabis, he is therefore securing himself an income [from it]."
Mitigating for Batlam, Ian Cook said: "This is a man who was akin to a gardener, though I accept he was going to receive a significant financial advantage.
"This was a man who always worked, then one day he got a telephone call from social services. His ex has considerable difficulties with alcohol - he stopped working that day and became a father.
"He may have made some mistakes in fatherhood, but these are the mistakes he is trying to rectify now."
Arguing for the judge to suspend any prison sentence, Mr Cook added: "This is a golden opportunity for him. This is the next stage in this man's progression."
Sentencing Bartlam, Judge Myerson said: "Anyone who does the maths understands that you are spending vrtually every penny of your benefits on cannabis for yourself.
"It's unfortunate that you are the sole carer of your little boy - it is not an example of good fatherhood.
"Social services were convinced that you were able to provide a situation in which [your son] was safe and nurtured.
"Only with that in mind, and as an act of what seems to me to be extraordinary generousity, I am prepared to set the starting point at two years and six months."
Once a sentence reaches this threshold, judges must consider whether it should be suspended.
"I accept that there is remorse," added Judge Myerson. "This is a fork in the road for you. We have seen evidence that you are genuinely prepared to go into business for yourself.
"I am prepared to give you that opportunity, and it is by the skin of your teeth. I am not anxious to punish an entire family for your behaviour."
Bartlam was given a two-year prison sentence, suspended for two years, as well as 150 hours unpaid work for producing a controlled drug of Class B.