Anthony Ray Hinton was going out of his mind on Alabama’s death row as he repeatedly churned over the iniquities of the shambolic trial that put him there. So he started a book club.

The prison warden wasn’t keen but after Hinton told him that it was better to have inmates applying their minds to reading than to ways of causing disruption, he reluctantly agreed. The book club began with a handful of death row prisoners meeting in the jail’s law library, and for a few years the numbers grew as word spread that reading was as good an escape as any of them could expect. But then the numbers shrank again as each of the members was executed until only Hinton remained of the original group. His cell was so close to the electric chair he could smell the burning flesh of the men he’d been debating the writings of James Baldwin and Harper Lee with.

They didn’t care whether I did it. They cared about the colour of my skin

All the while, Hinton awaited his own appointment with “Yellow Mama”, the generator that powered the electric chair, as one attempt after another to have the overwhelming evidence of his innocence heard got nowhere.

“The state of Alabama had every intention of killing me for a crime I didn’t commit,” said Hinton. “They didn’t care whether I did it. They cared about the colour of my skin. As the prosecutor said: ‘Even if we didn’t get the right one, at least we got one off the street.’ And that was referring to a black person only he didn’t use the word black.”

Hinton, who goes by Ray, was on death row for 28 years until the US Supreme Court overturned two murder convictions for which the evidence was, at best, flawed. Now he has written his own book, The Sun Does Shine: How I Found Life and Freedom on Death Row, recounting his long battle for freedom against an Alabama judicial system that showed no interest in whether it was planning to take an innocent man’s life.

I met Hinton, 61, at the rural Alabama home where he lived with his mother, Buhlar, when he was arrested in 1985. The photographs of Hinton at his trial show a slender man with a trimmed moustache. Now he is stocky with a greying beard to match his hair. A computer lies on a desk in the corner of the living room, but Hinton is wary of it, uncertain how to use all but the most basic of functions. He is not hesitant, though, as he tells his story with a visceral anger not only at his own tribulations but at the state of Alabama in the 21st century.

Free at last: Anthony Ray Hinton is greeted by family outside the Jefferson County Jail in Birmingham, Alabama. Photograph: Marvin Gentry/Reuters

Hinton, then aged 29, was in the back garden of his mother’s wooden house half an hour north of the industrial city of Birmingham, Alabama, when the police arrived to question him about the murder of a restaurant manager, John Davidson. Buhlar Hinton owned a handgun. The police took it for testing. State forensics experts said it not only fired the bullets that killed Davidson but had been used in two other shootings during restaurant robberies. In one of them another manager was killed.

Hinton was charged with two murders. What followed was a three-decade-long miscarriage of justice driven not by one bad cop or a single piece of flawed evidence but a system that conspired against justice.

There were no fingerprints and no evidence that the shootings at the restaurants were even related, but one police officer reached the incredible conclusion that all three crimes were committed by the same black man who “lived with his mom or auntie”.

Another detective on the case, Doug Acker, had been tried three years earlier for torturing black prisoners with electric cattle prods and beatings to obtain confessions, but was acquitted by an all-white jury. Hinton said Acker told him to sign a blank sheet of paper, assuring him it was just to confirm he had been read his rights.

“I wasn’t a fool. There was no way I was going to sign a blank piece of paper. I looked up at the men around me. They looked happy, excited. Maybe even a little jumpy, like you do when you have a big secret you are just dying to tell. In that moment, I felt the first real twinge of fear,” Hinton writes in his book.

Fully charged: Alabama’s electric chair, known as ‘Yellow Mama’. Photograph: Kiichiro Sato/AP

The prosector, Bob McGregor, was twice found by courts to have illegally discriminated against African Americans by excluding them from juries. Hinton took a lie-detector test on the understanding that no matter what the result it would be submitted as evidence at his trial. When he passed, McGregor blocked it from being used in court.

The prosecutor acknowledged to the jury that the only significant evidence against Hinton was the ballistics test. Hinton’s poorly paid public defender [defence lawyer], Sheldon Perhacs, did not prove up to the task. Among several grievous mistakes he made was to hire a visually impaired civil engineer with no professional experience of ballistics to test the bullets because he came cheap. The expert firmly said the tests did not link the weapon to the shootings, but then the prosecutor tore his credibility apart, belittling him for his poor eyesight and inability to use a microscope properly.

There was death and ghosts everywhere. The row was haunted by the men who died in the electric chair

Hinton’s boss testified that he was at work cleaning a supermarket warehouse and could not have committed the crime. But the jury still convicted the young black man and sentenced him to death.

The first few years of prison were tough. Hinton was so angry he refused to speak. “Every hour of every day, I imagined how I would kill McGregor,” he wrote.

Death row was its own particular hell, with inmates crying and screaming at night. “There was death and ghosts everywhere. The row was haunted by the men who died in the electric chair. It was haunted by the men who chose to kill themselves rather than be killed.”

In his time on death row, 54 men passed Hinton’s cell on their way to execution. He was kept going by his mother’s unfailing belief in his innocence and the support of a childhood friend, Lester Bailey, who never missed a visit all the years Hinton was in prison.

There is a joke on death row that capital punishment is punishment of men without capital. Hinton had no money and was stuck with Perhacs, who proved as incompetent in fighting appeals as he had at the original trial, losing in one court after another.

Speaking up: Equal Justice Initiative lawyer Bryan Stevenson speaks to the media after the release of Anthony Ray Hinton. He worked on Hinton’s case for 16 years. Photograph: Marvin Gentry/Reuters

Most of those on death row were African American, but there was a white prisoner Hinton soon learned was Henry Hays, a member of the KKK who beat to death a 19-year-old black man, Michael Donald, and hanged his body from a tree in 1981. Hinton confronted Hays about his racism, but came to realise that he had changed in prison and eventually saw him as a victim poisoned by the hatred of his father, Bennie Hays, a leader of the KKK in Alabama.

“Henry Hays was cheated all his life. He was cheated by his father who taught him to hate. His community taught him to hate,” said Hinton. “My mom told me, no matter what one does in life, he or she deserves some compassion and I knew Hays deserved compassion more than anybody.”

Hays was a member of the book club. One visiting day he called Hinton and another black prisoner over to meet his father, introducing them as his friends.

“His father refused to shake our hands and Hays told us later that his father told him: ‘Don’t you ever invite a nigger over to this table.’ Hays felt bad. I told him don’t feel bad for what your father is saying. Hatred is nothing but a form of cancer and it will eat you up.”

Hays was executed in 1997.

“On the night of his execution he finally admitted that all his life his father had lied to him, and that now he knows what love is,” said Hinton.

A white man of authority don’t ever want to admit to someone of colour they was wrong

The first step on the path to salvation for Hinton came after a decade inside. Hinton’s case was taken up by the Equal Justice Initiative in Montgomery, Alabama. Founded by a lawyer, Bryan Stevenson, it had a track record of overturning unjust convictions and in winning a supreme court ruling prohibiting the death penalty, or life without parole for young people under the age of 18.

Stevenson had new ballistics tests done by three experts that Alabama’s judicial system could not discredit, including the former chief of firearms testing at the FBI. The tests concluded the bullets in one of the three shootings definitively came from a different gun and the bullets in the others could not be matched. That alone should have been enough to overturn the conviction.

The Alabama courts would not be able to deny the credibility of the tests so it set about not considering them at all. At an appeal hearing, prosecutors dropped their original tactic of mocking the defence ballistics expert and now, playing for time, built him up as a credible witness whose testimony had been rejected by the jury. The appeal judge, who oversaw the original trial, took two and a half years to issue a ruling siding with the prosecution’s claim that to consider the new ballistics evidence would amount to allowing Hinton’s case to be re-tried.

Hinton regarded the ruling as political and racial.

“A white man of authority don’t ever want to admit to someone of colour they was wrong,” said Hinton. “They knew they had made a mistake, but you have this macho attitude: ‘I don’t have to apologise to no nigger.’”

Besides his books, Hinton sought other ways of mental escape. He imagined a world in which he married Halle Berry and then Sandra Bullock – he’s quick to assert there was nothing sexual in it – and was a regular for tea with the Queen of England.

“The way I looked at it, I went to death row for 28 years through no fault of my own and at no fault of her own she was born who she is. She didn’t have a say over being Queen. I looked at her living in this palace and in a way she is imprisoned just like I was. I often wondered what she’d do in order to survive that prison,” he said.

Eventually, with all avenues for justice within the Alabama system shut off, the path was open for Stevenson to take Hinton’s case to the US Supreme Court. In his petition, the lawyer called Alabama’s death penalty “a perverse monument to inequality”.

I looked at the Queen living in this palace and in a way she is imprisoned just like I was

The Supreme Court unanimously overturned Hinton’s conviction in 2015. Alabama didn’t have the evidence to retry him. Hinton finally left death row – and not in a body bag. “I need somebody to explain to me: why did it take 30 years to finally get it right? America should be ashamed. To say they have the best justice system in the world when every day race plays a part in who goes to prison, who don’t go to prison. The state of Alabama had every intention of killing me for a crime I didn’t commit. If the United States Supreme Court hadn’t intervened I wouldn’t be here today,” he said.

Hinton is back living in the house where his mother spent her last days convinced of her son’s innocence.

Since his release, he has travelled in the US and abroad, speaking about the injustice he endured, and how religious faith, his mother, and friendship helped him survive. Some find inspiration in the story of how Hinton retained his humanity and resilience on death row, encouraging fellow inmates with the book club and a sense of solidarity, or in the unswerving loyalty of Lester Bailey, the friend who never missed a monthly visit and who now lives a couple of houses away. Others are outraged and disturbed by such a blatant miscarriage of justice. But for Hinton, his talks also serve as a warning to other African Americans that this can happen to them. Black men are imprisoned just for being black men.

The state of Alabama is resisting Hinton’s claim for compensation on the grounds he hasn’t proved his innocence. That mindset tells him the injustice that so nearly took his life could happen again. It’s a fear of which he’s never entirely free.

When Hinton leaves the house these days he leaves a trail so he can prove where he’s been. He stands in the sight of video cameras and collects receipts from convenience stores. And he makes sure to call people several times a day on his mobile phone. “I’m told that the phone can tell what time and where I was at. And I’m hoping that if ever I was charged with anything again I could say, you can check my phone and hopefully it will clear me,” he said.

In the meantime, he’s working on meeting the Queen and Sandra Bullock.

The Sun Does Shine: How I Found Life on Death Row by Anthony Ray Hinton is published by Rider Books at £16.99, or £14.44 at guardianbookshop.com