Paper darkens as it grows old, but vellum just goes duller white, like the belly of a snake: looking at some of the manuscripts through which learning made its serpentine passage across the medieval world makes it obvious that you couldn't call those ages "dark". The library of The Royal Observatory in Edinburgh holds one of the finest collections of early astronomical books and manuscripts in the world, collected by Lord Crawford in the 19th century. He left them to the city on condition that they built an observatory to house them. Being civilised, the city fathers did. So there I was on Tuesday, touching the vellum of a 13th century manuscript of Alhazen, another of Aristotle, and then a first edition of Copernicus' De Revolutionibus and one of Kepler's Nova Astronomia. In the shelves on the wall were Galileo's works.

The frontispiece from a first edition of De Revolutionibus Orbi by Nicolaus Copernicus in the collection of the Royal Observatory, Edinburgh. Photograph: Andrew Brown/guardian.co.uk

We were meant to be making a radio programme – an interval talk for Radio 3 – but the producer and I and our guest Ken MacLeod just frolicked round that room of priceless books like salmon woken by a spate. Serious work was impossible for a while. There was nothing to say that was adequate in the face of so much beauty and so much history; for anyone who writes, the feel of a physical object which has been read for 800 years is a quite extraordinary thrill.

Alhazen is almost forgotten now, and Aristotle little read or acknowledged outside the Roman Catholic intelligentsia. But when those first manuscripts were only three hundred years old, the books which we all know have changed the world were published. First there was Copernicus – the library has a first edition of his book, although there are only 276 known in the world. Then came Kepler's Nova Astronomia and his Harmonice Mundi, the books in which he laid out his three laws.

These really are the books which ended the medieval world view. But what they did not represent was a struggle against religion, or "the church". Copernicus, of course, was a canon. Kepler was extremely devout. As a Lutheran, he was under suspicion at the court of the Catholic Holy Roman Emperor, Rudolph I; he was also under suspicion from his own side for being too friendly with the Calvinists at university. There's no doubt at all that he was trying – like Newton – to understand God when he was trying to understand the heavens.

What is more interesting is to consider the kind of understanding, and the kind of God, which he had in mind. And here, I think, the clue is to be found in the decorations and the illustrations of his books. The margins of Kepler's Harmonice Mundi contain numerous snatches of musical notation (he had read, and profited from, a book on music by Galileo's father). For Kepler, harmony was "geometrical proportions which he finds reflected everywhere, the archetypes of universal order, from which the planetary laws, the harmonies of music, the drift of the weather, and the fortunes of man are derived." to quote from Arthur Koestler's history of the cosmology, The Sleepwalkers. Koestler continues:

These geometrical ratios are the pure harmonies which guided God in the work of Creation; the sensory harmony which we perceive by listening to musical con­sonances is merely an echo of it. But that inborn instinct in man which makes his soul resonate to music, provides him with a clue to the nature of the mathematical harmonies which are at its source. The Pythagoreans had discovered that the octave originates in the ratio 1:2 between the length of the two vibrating strings, the fifth in the ratio of 2:3, the fourth in 3:4, and so on. But they went wrong, says Kepler, when they sought for an explanation of this marvellous fact in occult number-lore. The explanation why the ratio 3:5, for instance, gives a concord, but 3:7 a discord, must be sought not in arith­metical, but in geometrical considerations.

It's clear, I think, that what drove Kepler on through years and years of immensely tedious work – endless calculations of spherical geometry without even logarithms to help him – was the belief that there would be a harmony revealed, a single chord that might summon the whole universe to order and to resolve its horror. Without it, he would never have persevered, and never have discovered the three laws which bear his name.

But the laws, as we would now understand them, are buried in the beauty. What Kepler thought he had really found was something even more remarkable: to quote Koestler again,

Saturn, for instance, when farthest away from the sun, in its aphelion, moves at the rate of 106 seconds arc per day; when closest to the sun, and its speed is at maximum, at 135 seconds arc per day. The ratio between the two extreme velocities is 106 to 135, which only differs by two seconds from 4:5- the major third. With similar, very small deviations (which were all per­fectly explained away at the end), the ratio of Jupiter's slowest to its fastest motion is a minor third, Mars' the quint, and so forth. The extreme values yield in fact the intervals of the complete scale. But not enough: if we start with the outermost planet, Saturn, in the aphelion, the scale will be in the major key; if we start with Saturn in the perihelion, it will be in the minor key. Lastly, if several planets are simultaneously at the extreme points of their respective orbits, the result is a motet where Saturn and Jupiter represent the bass, Mars the tenor, Earth and Venus the contralto, Mercury the soprano. On some occa­sions, all six can be heard together.

I have no idea whether anyone has tried to write music based around this discovery; and I assume, though I have not checked it, that the subsequent discoveries of Uranus and Neptune (as well as the discovery, and then the undiscovery of Pluto) would introduce some horrible noise to the concert. But it was absolutely true as far as it went, and for Kepler the musical aspect was as true as the mathhematical one. Neither was a more profound description of reality, though both, of course, were deeper and truer than the world as it presents itself to our gross senses.

This isn't a claim that all scientists are fired by a passion for God, or by imagination. Of course they are all animated by their imaginations: everyone is. The dream of a world containing only facts is itself an imaginative construct. But to say this is to deny that there is a single imaginative temperament, or that imaginative people will come to see the world the same way. And it's manifestly not true that all scientists care for God; in fact I think the discovery of DNA was at least partly a consequence of Francis Crick's ferocious atheism.

A page from Harmonices Mundi by Johannes Kepler, showing musical notation, from the collection of the Royal Observatory, Edinburgh. Photograph: Andrew Brown/guardian.co.uk

So the longing for harmony drives only one possible scientific temperament, as it drives one style of religious thought. You can be a great scientist without that passion. I think Darwin is a good example: he had what Stephen J Gould called a pluralist imagination. Conversely, you could be a driven by a passion for order and harmony and partly for that reason be a bad scientist. But the division goes back at least as far as Plato and Aristotle. But it does not divide science from religion, nor even good science from bad. It just suggests something of the extraordinary richness of history, and of the complexities of our engagement in the world.

Kepler finished Harmonice Mundi in 1618 just as the thirty years' war was breaking out. The population of his native Germany would be reduced by a third in the rest of the century. Nothing so terrible would happen there again until 1945. But at the end of the book, he allowed himself one great triumphant shout:

In vain does the God of war growl, snarl, roar, and try to inter­rupt with bombards, trumpets, and his whole tarantantaran. .. Let us despise the barbaric neighings which echo through these noble lands, and awaken our understanding and longing for the harmonies.