I mistakenly switch from past to present tense when writing. How do I avoid this?

You might find it helpful to start a writing session by reading a page or two that leads up to the scene you’re writing. That may fix tense in your mind as continue. You might also do spot checks as you write. Pause at timed intervals—perhaps every fifteen minutes—and ask yourself if you’re still using the correct tense. A quick check of several verbs will let you know if you’re consistent.

In the end, the only way to assure yourself of consistency throughout the entire piece is to read and re-read and revise any lapses you find. When the time comes, do a revision where you only attend to tense. That will help you clear out the clutter of other concerns and keep your attention to tense heightened. You’ll want to consider every individual verb as well as what tense does to the entire sentence. You may find changes to the verb also necessitate other changes for clarity, tone, or flow.