Prince William spent a night sleeping rough on the streets of London last week to experience being homeless.

The royal bedded down in a sleeping bag next to a group of wheelie bins near Blackfriars bridge in an event organised by the homelessness charity Centrepoint.

The organisation's chief executive, Seyi Obakin, slept next to the prince on their makeshift bed and said the "hairiest moment" came when they were almost run over by a road sweeper.

He wrote on the Centrepoint website: "For me, it was a scary experience. Out of my comfortable bed. Out there in the elements. Out there on an extremely cold night, with temperatures down to minus 4. And it was the same for Prince William. But he was determined to do it as [Centrepoint] patron in order to raise awareness of the problem and to be able to understand a little better what rough sleepers go through night after night."

Obakin challenged the prince to experience being homeless for one night after William, in a speech to mark Centrepoint's 40th anniversary this year, urged the charity to redouble its efforts to end youth homelessness.

The pair were joined on the sleep-out by William's private secretary, Jamie Lowther-Pinkerton. In a grainy photograph released by Centrepoint, the prince – dressed in jeans, trainers, a hooded top and a hat – can be seen standing next to Obakin with their bedding around them.

"We took as much precaution as possible – finding a relatively secluded spot in an alleyway, shielded partly by a collection of wheelie bins. But there was no shielding from the bitter cold, or the hard concrete floor, or the fear of being accosted by drug dealers, pimps or those out to give homeless people a 'good kicking'," Obakin wrote.

"One of the hairiest moments occurred when we were almost run over by a road sweeper which simply didn't see our small group huddled together, which just goes to show how vulnerable rough sleepers are. I have never been happier to welcome the break of dawn."

The group bedded down just after midnight last Wednesday and slept until 6am, when William rose for a tour of the West End to see where many young homeless people were sleeping.

A St James's Palace spokesman said the trio got little sleep and dozed for short periods. "Prince William took away from the experience the importance of tackling all the issues that cause people to be homeless and stay homeless, from drug dependency to mental health problems," the spokesman said.