Change Your termostat,There is 3 different temp termostats. 90",180',200,.If Your code is P0128.Check in any autozone or advance auto parts.for free.they will let You know witch one You will need.I had a same problem.
Is the code causing the check engine light a P0125? If you can give us the exact code we can narrow down the area causing the light to come on. Ensure coolant level is ok. Does the heater work well and the engine warm up quickly allowing the temperature gauge to read and run around the middle of the gauge (or just below the middle.) I suspect the cooling system thermostat has failed and has stuck open allowing the engine to run colder than it should resulting in higher pollutants emitted from your car, slightly increased engine wear and decreased fuel economy.
Pat is likely correct about the thermostat, but we would need to know the stored code to be sure. Volvo has upgraded software to help address this code on the S40 on the early models, but I'm not sure if it applies to your S40 though. You will need to take the vehicle to a good Volvo Independent shop or the dealer to have this software checked and/or updated.