Mazda B2500

why does the cel light keeps comong back on, after I corrected the problem? (when I read the code it reads the same problem which i fixed by replacing the part)

There is something to do other that change the sensor. Tell us the trouble code and someone will be glad to tell you what to do about it. OK?

The code I get is PO402, so I replaced the EGR flow sensor, erased the code, it ran well for a while, then the light came on again. It sometimes goes away after a day or a few hours, and sometimes it doesn’t come back on for weeks, but I noticed that the pinging comes back on when the light is on

I think you should have left the flow sensor alone. It was sensing that there’s too much EGR flow and that’s what the problem is, not the sensor itself. Excessive EGR flow can come from a plugged exhaust system (eg bad catalytic convereter) or a failing EGR valve.

EDIT: Does the truck feel like it’s down on power? If so, that would be indicative of a plugged exhaust system.

Always keep in mind that those codes only tell you what was sensed. If the computer gets a report that something is not right, then it reports the report to you. What is not right could be a sensor, or it could be what the sensor is sensing.