For a research project, we are needing to control a motor's speed and direction. The setup we are using comes from Adafruit, attached below. We were able to connect the motor successfully, and everything is running well, however, when we go into the Serial Monitor to change the speed, nothing happens. How can we control the speed? Thank you ![]()
Please post the code here. Schematics might be useful.
Is your serial monitor set to no line endings?

To see what is going on, put a serial print after the serial read to see what you are getting from the serial monitor.
Code:
/*
Adafruit Arduino - Lesson 13. DC Motor
*/
int motorPin = 3;
void setup()
{
pinMode(motorPin, OUTPUT);
Serial.begin(9600);
while (! Serial);
Serial.println("Speed 0 to 255");
}
void loop()
{
if (Serial.available())
{
int speed = Serial.parseInt();
if (speed >= 0 && speed <= 255)
{
analogWrite(motorPin, speed);
}
}
}
I wired up the project to my Uno and loaded the code. The project did not work correctly with line endings. It did work when line endings set to No Line Endings. Did you change the line endings in serial monitor?
What is the exact part number of your transistor?
Post clear photos of your wiring.
Read the forum guidelines to see how to properly post code and some good information on making a good post.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.
You can go back and fix your original post by highlighting the code and clicking the </> in the menu bar.

Yes, I have selected the 'No line ending' section, but it seems as though my entry disappears when I hit send. as for the serial print, do you have any suggestions as to what to type in the parentheses? (I apologize, this is my first major Arduino project, so I am still learning.)
For the transistor I am using, the parts number is 2126 I believe. The information on the Transistor is as follows: TWP, 36GZ, #2126, 402102.
Below is my wiring:
I fixed a few things and added the serial prints that show what is received from serial monitor.
const byte motorPin = 3; // put the byte in flash
void setup()
{
//pinMode(motorPin, OUTPUT); //********** not needed
Serial.begin(9600);
// while (! Serial); //********** not needed for Uno
Serial.println("Speed 0 to 255");
}
void loop()
{
if (Serial.available())
{
int speed = Serial.parseInt();
Serial.print("you entered "); // ***** added line
Serial.println(speed); // ***** added line
if (speed >= 0 && speed <= 255)
{
analogWrite(motorPin, speed);
}
}
}
The 2SA2126 or 2SK2126 transistor is a PNP transistor and will not work with that wiring. The project calls for a 2N2222 NPN. You could substitute a 2N3904 or or other equivalent NPN. What, other, transistors do you have.
You really should have mentioned, in the original post, that you did not use the transistor specified on the project page.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.

