Darwin more or less believed in dim pre-consciousness of insects, but this is not in conflict with the idea that consciousness is a product of evolution, in fact it supports it. Darwin was a gradualist, he did not believe in qualitative leaps with new traits jumping into existence in full glory, evolution proceeds mutation by mutation, with no bright lines, in particular between consciousness and lack thereof. So if we find consciousness in humans it must have precursors in animals, this does not preclude it from evolving, nor does it imply panpsychic mind dust that enables it by permeating everything. Consciousness emerges in evolution, but gradually, seems to be Darwin's position. His friend and popularizer Romanes was a panpsychist, but there is no evidence that Darwin was.
Young Darwin struggled with the questions of how far down the evolutionary tree such notions as free will and consciousness might extend in his early notebooks reviewed in Smith's Charles Darwin, the Origin of Consciousness, and Panpsychism. Here is from the notebooks:
"Planaria [flatworms] must be looked at as an animal, with consciousness, it choosing food - crawling from light - yet we can split Planaria into three animals and this consciousness becomes multiplied... Hence a sensorium which receives communication from without and gives a wondrous power of willing (can willing be used without consciousness, for it is not evident what animals have consciousness). How does consciousness commence? Where other senses come into play, when relation is kept up with a distant object, when many such objects are present. This can take place and man not conscious as in sleep, or in sleep is man momentarily conscious but memory gone? Where pain and pleasure
is felt, where must consciousness be? How near in structure is the ganglionic system of the lower animals and the sympathetic in man? Can insects live with no more consciousness than our intestines have? ...the whole is a mystery."
It is clear that Darwin saw consciousness not as a "by-product" of evolution, but as a trait subject to it, but was not at all sure as to its presence in various animals, or specific mechanisms of its emergence. We can hardly blame him. His pessimism is reiterated in the Descent of Man, but again while explicitly naming the mental as a product of evolution:
"In what manner the mental powers were first developed in
the lowest organisms is as hopeless an enquiry as how life itself first
originated. These are problems for the distant future, if ever they are to
be solved by man".
Darwin handed over his notes and manuscript on comparative psychology to Romanes, who used some of it in his book Mental Evolution in Animals, where he is even more explicit than Darwin:
"If the doctrine of Organic Evolution is accepted, it carries with it, as a necessary corollary, the doctrine of Mental Evolution... Starting from what I know subjectively about the operations of my own individual mind, and of the activities which in my own organism these operations seem to prompt, I proceed by analogy to infer from the observable activities displayed by other organisms, the fact that certain mental operations underlie or accompany these activities."