I am trying to use a potentiometer to control the speed of my dc motor based off where the potentiometer is at. When i use the is code and circuit my motor hums and the transistor gets very hot. I am using a 222A pn transistor.
That base resistor should be 150 ohms, not 2k2 (*). And if your motor takes more than about 0.5A
you need a logic-level MOSFET, not a 2N2222 which has limited current ability.
(*) To use a transistor as a switch you have to saturate it. A transistor with a gain of say 200 in the linear
region does not have anything like than gain in saturation. Normally you'd assume a gain of 10 to 20.