Explain how a galvanometer functions, using electromagnetic principles.