"Do you know any gay people?" Sir Ian McKellen asks. Silence. Heads shake. "Well, you do now. I'm gay." It's my turn to speak up. "You know two now. I used to go to this school – and I'm gay," I offer. "You know three now," a sixth-former chips in. The other pupils don't look too surprised, and he seems admirably comfortable in his sexuality. Silence. Then: "Erm. Well. You know four now." Heads shoot around to see a uniformed boy, leaning close to McKellen. Mouths fall slightly open – including mine – but nobody speaks. Then McKellen says, in that mellifluous voice of his, "Well. How about that? It turns out we all know quite a few more gay people than we thought we did."

This is the third month of McKellen's nationwide "role model" tour of secondary schools on behalf of Stonewall, the gay equality charity that he co-founded, and which I work for, and the two of us have come to Hundred of Hoo comprehensive in Kent, which I left over a decade ago.

It has become a familiar scene for him. "My school visits are often rewarded by people coming out," he says. "And I don't just mean pupils – I've heard staff coming out to their heads on my visits, too."

McKellen obviously has a powerful effect on the schools he visits; how does this make him feel? "A bit overwhelmed – and privileged," he says.

Gandalf has worked his magic in 54 secondary schools over the last two years. His dream? An education system free of the homophobia that has plagued it for years – and a curriculum that fully includes lesbian, gay and bisexual people.

Hundred of Hoo has recently come out of special measures and is no stranger to underachievement. McKellen and Stonewall see visits to schools like this as crucial to ensure that not one ounce of potential is lost for today's gay pupils – especially for those who under-perform because their confidence has been battered by bullying.

Homophobia was rife when I was a pupil. "Freak", "queer" and "disgusting" were familiar words, aimed at anyone, like me, who was perceived to be gay. Consequently, nobody dared to come out. This created an un-virtuous circle; teachers could see no reason to address gay issues in lessons when there appeared to be no gay pupils. It wasn't until I left that I discovered three of my best school friends were also gay. We had been too scared even to admit it to each other.

The notorious "section 28" law, introduced in 1988 when Margaret Thatcher was prime minister, was to blame for much of this. This made it illegal to "promote" homosexuality in schools. So homophobia festered unchallenged for 15 years until its repeal in 2003. But the hangover remains. Recent YouGov research for Stonewall found that nine in 10 secondary school teachers say their pupils experience homophobic bullying, but nine in 10 have never received any training on how to tackle it. Ninety five per cent of teachers hear the phrases "you're so gay" or "that's so gay". Homophobia is so commonplace that "gay" has entered the school vernacular as a synonym for anything inadequate.

But I hardly recognised this school upon my return. Gay issues are very much on the agenda – and pupils are refreshingly honest about their prejudices. "We Googled you yesterday Ganda … I mean Sir Ian!" says a 12-year-old girl. "We were well surprised when we found out you were gay, because you're nothing like Alan Carr!" McKellen replies that gay people come in all shapes, sizes and personalities, just like straight people. He knows Alan and he is just like that off-screen, too. The most important thing is that he can be himself.

The class listens eagerly as the actor tells them he didn't come out publicly until 1988, at the age of 49, because section 28 was being debated in parliament. "Did you worry it might destroy your career?" a pupil asks. Yes, but that was a risk he was willing to take, McKellen says, as he explains why gay visibility was so important at that time.

How has he found the pupil reactions? "Until I visited secondary schools recently, I hadn't realised how much anti-gay bullying goes on," he says. "By talking frankly about my own life as a gay man and listening to the concerns of staff, students, parents and governors, I hope the visits may make a difference and also give confidence to gay students about their lives in the future."

Homophobic language

It's time for the all-school assembly, the grand finale of Gandalf's visit. "I'm not useless," McKellen asserts in my old school hall, "but when you use that word as an insulting adjective, that's what you're saying about me. So please, watch your language. Because if you don't, you mightn't watch your actions…" He goes on to tell how Ian Baynham was recently killed in a homophobic hate attack by teenagers. "The girl who stamped on his head might have used 'gay' to mean anything rubbish and useless. And that probably convinced her that gay people were rubbish and useless – and don't deserve to live."

This has a profound effect on two year 10 friends, who tell me: "We didn't realise calling things 'gay' could offend someone. It was touching when he talked about never being able to tell his mum he was gay. One of our best friends is gay and he gets abused for it. We hope it will stop now."

McKellen also visits lessons to promote Stonewall's curriculum guidance called "Oh no! Not the gay thing!", which advises teachers how to integrate gay issues into classes. In science lessons it can be a relief for gay pupils to learn about same-sex attraction as a natural fact in other species. And same-sex relationships can make pupils think carefully about grammar in modern languages.

Stonewall's new campaign in schools also includes Lance Corporal James Wharton, who, at 24, is the new poster boy for modern gay equality: he was the first openly gay soldier to appear on the cover of Soldier, the Armed Forces' magazine. Wharton will be touring schools with Stonewall over the next three months. There are also plans for a school intervention from a celebrated high-achieving lesbian or bisexual woman role model – who knows, she may be coming to a classroom near you soon.