Generally, you can be convicted of a DUI/DWI for taking prescription medication or a drug that's legal in your state. It just depends on whether you've taken enough of the drug or medication to be "under the influence" or "intoxicated" as defined by state law.
All states have "per se" DUI laws that prohibit driving with a certain amount of alcohol in your system—typically, a blood alcohol concentration (BAC) of .08% or more. And some states also have per se DUI laws that make it illegal to operate a vehicle with a certain concentration of drugs in your blood. However, per se drug DUI laws typically apply only to common illegal drugs like cocaine, methamphetamines, and marijuana.
But there's a second type of DUI charge that's based on actual impairment (aptly called an "impairment DUI") rather than the measured amount of drugs or alcohol in the driver's body. Impairment DUI laws focus on the effects that the ingested substance had on the driver—not necessarily what the substance was or how much of it was in the driver's system. So, cases involving prescription drugs like painkillers and benzos are typically charged under impairment DUI laws.
To prove an impairment DUI at trial, the prosecution needs to show the driver was "under the influence" or "intoxicated" as defined by state law. States differ on the level of impairment that prosecutors must prove. Some states, for example, require proof that the driver was "substantially affected," while in other states, evidence of "any effect" is enough.
A driver who's arrested for a DUI based on prescription drug use may have a limited defense. A handful of states will let drivers off the hook if they can show they were impaired as the result of taking a prescription medication as prescribed by a licensed physician.
Quite a few states now have at least some legal marijuana use—either recreational or for medical purposes. But just like with prescription medications, legal marijuana use can result in a DUI conviction based on impairment. Some states also have per se marijuana laws that prohibit motorists from driving while having a certain blood concentration of THC, the psychoactive ingredient in marijuana.
If you've been arrested for a DUI related to lawful drug use or medication that a doctor prescribed to you, get in contact with a knowledgeable DUI lawyer in your area. An experienced DUI attorney should be able to tell you how state law applies in your situation and whether you have any viable defenses.