THERE are times when the great Irish writer Sebastian Barry, who has performed a sort of archaeological literary dig on his own family's secret history in a shelf-full of plays, poems and novels, asks himself what the hell he is doing.

"I stop and think I never intended to do this," he says, a note of wonder in his voice as he considers his literary output, based on shards and scraps of dark memories he has unearthed and dragged into the light. Now 58, he's forever telling himself that what he's doing belongs more to psychiatry than literature. After all, writing is "an illicit occupation".

"Why would you do that for 35 years, write these things? I have no idea - it's shocking," he says, before repeating his recent statement that the only time he's seen himself "properly portrayed on television" is in the Scandinavian crime thriller, The Bridge. "I am Saga Noren [the socially inept, frighteningly honest, emotionally detached detective]. It's the weight of all that darkness," he adds, more seriously, referring to his family's misfortunes and his self-diagnosed Asperger's. "I have to talk to somebody, maybe a psychiatrist?"

Yet, continues the Dublin-born, Booker Prize-nominated author, there is grace in writing about your own family. "Like a robin, or indeed any creature, you listen to your innate birdsong. Still. What sort of creature writes this?" he asks, indicating his latest novel, The Temporary Gentleman. "What sort of robin does that? I ask you!"

What sort of "robin" indeed? A beguilingly warm and humorous one of "grandfatherly years", whose eloquent conversation sings with lyrical metaphors and poetic similes. One unable to read until he was eight but who spent "that spot of trouble known in Ireland as childhood" listening to bedtime stories told by his maternal grandfather, Jack O'Hara, tucked under an eiderdown in the room they shared in Jack's Munster mansion. There were many rooms, many lodgers. "Hence the shared bed. We lived in chaos - although, given recent revelations in Ireland, I must stress the innocence of the arrangement." Jack also gave his children emotional chaos. "But then the happy Irish childhood isn't worth a candle. Ask Frank McCourt!"

Jack McNulty, the pseudonymous Temporary Gentleman is, of course, Barry's grandfather. The title refers to his temporary commission in the British Army during the Second World War, when he worked in bomb disposal. "He became a 'temporary gentleman'. A weighted, rather wonderful phrase." This is Barry's third novel to dig deep into the McNultys' buried secrets. It is the story of his grandfather's life that he would have written himself, in the account book kept for that purpose. Sadly, his grandfather filled only three pages because he couldn't bring himself to write about his mother's shaming illegitimacy.

Like the real Jack, the fictional one is brother to Eneas, from The Whereabouts Of Eneas McNulty (1998) and brother-in-law to Roseanne from Barry's masterly, Costa award-winning The Secret Scripture, about to become a Hollywood movie starring Vanessa Redgrave, Jessica Chastain and Jonathan Rhys Meyers.

"My beloved, cherished, fictional Jack!" exclaims Barry. The Temporary Gentleman is his second novel about war - A Long Long Way (2005) told of Willie Dunne, a young Irishman who fought in the First World War, based on another family member's story. However, The Temporary Gentleman is actually about love - "true human love but not true love. Love as it appears to people who get married and before misfortune befalls them and they're devoured by drink."

The troubled, dissolute Jack McNulty sits in a cottage in Accra in 1957, where he was stationed during the war and later as a UN observer, relating his testament and his ferocious love for his dead wife, Mai Kirwan, "with her black eyes, hair as black as worry". She was a great beauty, "a woman replete, laden with gifts, musical, athletic, clever as a general". They had two daughters, then Jack drank and gambled away her family mansion. When he enlisted in the army, she took to drink too, as did the real Mai, who died at the age of 53, two years before Barry was born, and about whom he wrote his 1998 play, Our Lady Of Sligo.

"The last half of their marriage was a war zone, providing my mother and her siblings with an horrific childhood," says Barry. (His mother was the distinguished Irish actress Joan O'Hara, who died in 2007.) "I was too hard on Mai when I wrote about her before. Now when I think of the ordinary glory of herself and her potential so poisoned by alcohol, it makes me f***ing sad and it makes me angry. But how do you endure your story, how do you survive your own tale? When you read a novel you get such a privileged view of a life - you are able to discriminate among the dust and ashes to discover when the embers caught flame.

"Privately, my relationship with my characters is based on gratitude. If I can capture the birdsong of my characters, then I'm in the bush, within the realm wherein they resided gloriously or ingloriously all their days. I'm very aware of Mai's presence as a restored person. She's been given back the dignity of her gifts. She was locked for so long in this prison of story. Now, I feel her around the place. She's a fierce presence. She's not against me, I sense that she doesn't mind the book. I think she minded the play - there was a succession of disasters when it was on; I knew she was fighting back. This time, good things have surrounded the music of this book."

Barry, who lives at Tinahely in the Wicklow mountains with his wife, the actress-turned-screenwriter Alison Deegan, and their three children, began writing family plays in the 1990s without quite meaning to and that shifted "inconveniently" into novels. He couldn't write The Whereabout Of Eneas McNulty as a planned monologue. It morphed into a novel and that book reached to his next, Annie Dunne, who was first encountered in his play The Steward Of Christendom, based on the life of his great-grandfather Thomas Dunne.

"There are two sides of my family, the Dunnes and Barrys and O'Haras, I use the name Dunne, but Kirwan or McNulty for the Barrys. Now there's this sixth book, The Temporary Gentleman. Three of my novels are Dunnes, three McNultys. There's some mysterious symmetry going on but your God - who does not exist - does work in mysterious ways. All these books sing to each other. They're in conversation. Because I get to them every three years, sometimes characters' ages are different, events a little bit other, which I contentedly leave because things told in the hour in which they are told will be told in a certain way. To pretend you had the spirit of another night in the telling would be wrong.

"The huge task was to love them all. Writing about people who have lived, you have to adore them. I did adore my grandfather, although I didn't know Eneas. I knew Annie Dunne very well; I didn't know Mai. How real she is to me, this woman I never met! I know what she put my mother through so I could be forgiven for hating her. Perhaps I could even be forgiven for writing about them both viciously, but that has not been the task with all the books and it's better for me that it's not so. However, that is what makes them hard to write."

Was Jack unhappy with his grandson's mining the family's secrets? "It was more I was not happy with him not being happy. I'd no idea I could offend him. I was 22 when I wrote some short stories, thinking everything was out in the open. When you consider the secret history of Ireland, it is all about hidden things, what happens in rooms. Even at that age I realised that everything had to be taken out into the light and examined in sunlight.

"He called me to his house. He was very upset, very brutal. His reaction was, 'I am going to punch this boy in the soul.' He felt I'd delivered him a sucker punch. He said, 'How do you know all these things?' All the stuff he had concealed my mother had told me all my life, much embellished I later discovered. When I consider the damage their unhappy marriage did to my mother and subsequently to my sister, I say, 'I am sorry, beloved Papa, but it's my story too.' He wanted to patch it up when my sister married. I responded in a foolish, haughty way. My crime is I didn't want to do it then. He died two or three years later.

"I would say to you and to him, because he's probably floating around this office, no day has gone by without thinking about him, properly and fiercely loving him. I don't feel guilty about writing about him, I feel a glorious pride in him. I've been looking around for the malevolent stare or the hard word even inside me; I haven't had it. There's been more a sense of seeing double magpies everywhere, a sense of cheering on. A sort of 'F*** it anyway, tell it!'

The Temporary Gentleman by Sebastian Barry is published by Faber and Faber, £17.99