My check engine light has recently began illuminating and then turning off, why? on 2005 Cadillac STS

My check engine light will illuminate for more days than not but it comes on for a few days and then it will turn off for a day. It seems to, possibly, come back on after the car heats up or has been driven 30 to 40 miles. I replaced the gas cap already.

Asked by for the 2005 Cadillac STS
When light turns on take to your local autozone for free check. This will give you good direction to go in.