OF all the castles in Scotland, the most famous is undoubtedly Edinburgh Castle.

I think it is entirely fitting, therefore, that I close this 12-part series on Scotland’s built heritage with the greatest of all our historic buildings.

From next week, as I have promised, I will write a separate four-part series on the built heritage associated with transport – road, rail, air and waterways.

Regular readers will know that in this series I have made my own choice of the best and most important parts of our built heritage, all of which I have visited. I did not expect every reader to agree with my choices but they all had a story to tell, and all have made their contribution to our history.

So now to the greatest of them all. Edinburgh Castle has been at the centre of Scotland’s history for many centuries. No other single building or group of buildings has played such a vital role, not even the old Scottish Parliament or the new one – though I may change that view when Holyrood hosts the return of our independence.

Edinburgh Castle is now in the care of Historic Environment Scotland (HES) and I have relied on that agency’s considerable research for factual provenance. Any mistakes are mine, it should be noted.

The Castle Rock has been in existence for around 350 million years, a plug of volcanic rock that mostly stands some 260ft (80m) high above the city centre. The summit of the Rock is about 430ft (130m) high but erosion, especially during the last Ice Age, left the cliffs to the north, south and west of the Rock standing sheer while the “crag and tail” from what is now the esplanade runs eastward down the Royal Mile.

The National: Surprise of Edinburgh Castle, The Scots, led by Thomas Randolph, captured Edinburgh Castle from the English, 1314, Illustration from John Cassell's Illustrated History of England, Vol. I from the earliest period to the reign of Edward the Fourth,

Archaeological excavations have proven that the Rock was used as a fortification as far back as 900 BC, and it has been in human use ever since, making it the longest continuously inhabited place in Britain.

We know precious little about what happened on the Rock in prehistoric times but we can surmise it was settled permanently during the Iron Age, probably as a centre for the tribe which became known as the Votadini. The tribe’s capital was at the Traprain Law hill fort in East Lothian and its domination of the south-east of Scotland was recorded by the mathematician, astronomer and geographer Ptolemy, who knew them as the Otadini.

A Brythonic-Celtic tribe, the Votadini appear to have co-existed with the Romans but after the empire abandoned Britain the Votadini gradually moved their headquarters to the Castle Rock which became known as Dyn Eidin, the fort of Eidin, when the Gododdin people, descendants of the Votadini, ruled over the eastern part of what is now the Lothians.

The earliest written reference to a fort on the Rock is seen in a poem Y Gododdin, written much later but placing it as a residence in the late sixth century of Mynyddog the Magnificent, the Gododdin warrior who died fighting the Angles of Bernicia. They invaded from the south and by 638 had conquered the Gododdin, renaming Dyn Eidin as the burgh of Eidin, or Edinburgh.

The various kings of the united Scots and Picts from 843 onwards preferred to have their capitals north of the Forth and it wasn’t until the reign of Malcolm III, Canmore, that a royal residence was established on Castle Rock. We know that because his queen, Saint Margaret, was living there in 1093 when she was told of the death of her husband and her son, Edward, fighting the English at the Battle of Alnwick.

READ MORE: Edinburgh Castle in embarrassing Scottish king blunder

Another of their sons, Edgar, briefly ruled over Scotland and seems to have built some sort of castle on the Rock because he is recorded as having died there in 1107. His brother, Alexander I, succeeded him and may have developed the castle, though he preferred Stirling for his court.

Another brother, David I, succeeded to the throne in 1124 and immediately and significantly developed Edinburgh Castle as a royal residence. He formally made Edinburgh a royal burgh and it became his capital after he founded Holyrood Abbey in 1128.

IN a very real sense, then, Edinburgh Castle was the foundation stone of the city of Edinburgh and the evidence of King David’s attachment to the place can be found inside the castle to this day in the form of St Margaret’s Chapel erected around 1130 by the king in honour of his mother – it remains the oldest building in Edinburgh. David moved frequently between the castle and the royal lodgings at Holyrood, thus creating the Royal Mile.

There are records of an early form of parliament taking place in the castle around 1140, and with Edinburgh growing and becoming the most important city in the kingdom, King William the Lion lived in the castle until his capture by the English whose king, Henry II, imposed the humiliating Treaty of Falaise on William that included choosing the Scottish king’s wife – she was Ermengarde de Beaumont, and the castle was her dowry.

It was inevitable that Edinburgh would play a huge part in the Wars of Independence and so it proved when Edward I of England invaded Scotland in 1296.

In the first of a European-record 23 sieges sustained by the castle, Longshanks marched his army from defeating the Scots at Dunbar to capture the castle after just three days. He removed royal records and the regalia which joined the Stone of Destiny – or was it? – in London as war booty.

A garrison of 300 English soldiers then occupied the castle until 1313 when Robert the Bruce’s great ally Sir Thomas Randolph led just 30 men on a night raid up the cliffs and over the walls to capture the castle from within.

There is a plaque on the castle to record the deed. It states: “To commemorate Thomas Randolph, Earl of Moray. A distinguished soldier and diplomatist who recovered this castle in 1313 after it had been for 20 years in the hands of the English.’

Ever cautious, the Bruce had the castle demolished to deny its use to the English as they came north under the hapless Edward II to be defeated at Bannockburn. His son Edward III’s army backed the insurrection of Edward Balliol and captured the remains of the castle in 1334.

The English king paid for new walls, only for Sir William Douglas to recapture it in 1341. Disguised as merchants, his men prized open the castle to allow the Edinburgh citizenry inside where they slaughtered the English.

On his return from imprisonment in England, the Bruce’s son, King David II, rebuilt the castle entirely with David’s Tower named after him. He died in the castle’s royal apartments in 1371.

The Stewart dynasty would make the castle their fortress and residence and in 1440 the 10-year-old King James II got caught up in the infamous event known as the Black Dinner.

In one of the interminable squabbles between the powerful aristocrats, the Keeper of the Castle, William Crichton, arranged for William, 6th Earl of Douglas, aged 15, and his 12-year-old brother David to come to dinner in the castle. The two brothers were accused of treason, subjected to a mock trial and beheaded on Castle Hill. The event partly inspired the Red Wedding scene in Game of Thrones.

JAMES II loved artillery and imported the giant cannon Mons Meg from the continent. The great weapon was not far from James II when another cannon exploded and mortally wounded the king at the siege of Roxburgh Castle in 1460. Mons Meg itself burst in 1681.

James III made several improvements to the castle, such as the Crown Square, as well as formally making it the seat of government. His son, James IV, built the Great Hall that still stands much as it was during his reign that ended at Flodden Field in 1513.

The people of Edinburgh improved the city’s defences in anticipation of an English invasion but that did not come until 1544 when the forces of Henry VIII laid siege to the castle as part of the “Rough Wooing” in which the English were trying to force a dynastic marriage on the infant Mary, Queen of Scots.

After the death of her first husband, the young King Francois II of France, Mary returned to Edinburgh and mostly lived in the Palace of Holyroodhouse. But after her secretary David Rizzio, or Riccio, was murdered there by associates of her husband and king consort Henry Darnley, the pregnant queen moved to the castle for safety.

It was in her chamber, preserved even now, that Mary gave birth to her son James on June 19, 1566.

The castle could thus be said to be in at the beginning of the Union, as Mary’s son grew up to be James VI of Scotland and James I of England, crowned as such in 1603 when the royal court moved to London.

The “Lang Siege” of the castle had taken place during the civil war that followed Mary’s abdication, when the garrison under Sir William Kirkcaldy of Grange held the castle for Mary between 1571-3, the longest siege eventually ending when Regent Morton imported English artillery and bombarded the occupants into surrender. Morton then directed the rebuilding of parts of the castle and caused the famous Half Moon Battery to be built.

No more royal occupants resided permanently at the castle, though the Royal Palace was beautifully restored for the sole visit of home by James VI and I in 1617.

Another siege was conducted by the New Model Army of Oliver Cromwell during his invasion of Scotland in 1650. In keeping with his dislike of monarchy, Cromwell ordered the Great Hall to be used as a barracks. Eventually restored to King Charles II, but then captured by forces loyal to the usurpers William II – III of England – and Mary II, the castle did duty as a military base and a prison.

The Jacobite Rising of 1715 almost saw it captured by the army of James Edward Stuart, the Old Pretender, but his son Bonnie Prince Charlie preferred to base himself at Holyrood in 1745. The Jacobites did make a half-hearted attempt to besiege the castle, and the question must be asked that if Charles Edward Stuart had been able to capture it, might he not have based himself there in all its impregnability and ended the Union as he had pledged to do?

After the 1707 Union, the Scottish Crown Jewels, the Honours of Scotland, were moved deep within the castle and became out of sight, out of mind. It was only when Sir Walter Scott became famous that he was able to persuade the authorities to allow him to search the castle in 1818 and find the Honours, which are now on display in the Crown Room. It was Scott who argued

that the castle should be refurbished and opened to visitors, thus founding, some would say, the Scottish tourist trade.

The modern history of the castle is very much linked to the military and its use as a prison – for prisoners of war during the Napoleonic Wars and later for prisoners of conscience such as the Red Clydesiders John Maclean and David Kirkwood, incarcerated in the castle for their anti-war protests during the First World War.

The castle is nowadays the most popular paid-for tourist attraction in Scotland and houses Scotland’s National War Museum. To my mind, however, the most beautiful building in the castle is the Scottish National War Memorial, designed in the 1920s by Sir Robert Lorimer.

Also dating from that decade are the two statues of Robert the Bruce and Sir William Wallace, the latter inspiring screenwriter Randall Wallace to write Braveheart. But that’s another story …