[SOLVED]Error message when trying to compile Ladyada's DC motor control tutorial

Hi there. I try to control a DC motor as mentioned in the Lady Ada’s DC motor control tutorial.

Here is the program which I try to compile.

#include <Arduino.h>
/*
  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 = 100;
        if (speed >= 0 && speed <= 255)
        {
            analogWrite(motorPin, speed);
        }
    }
}

When I try to compile the code I get an error like this.

$ make
mkdir -p .dep/./
/usr/bin/avr-g++ -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=100 -DUSB_VID= -DUSB_PID= -I. -Iutil -Iutility -I /usr/share/arduino/hardware/arduino/cores/arduino -I /usr/share/arduino/hardware/arduino/variants/standard/ -I /usr/share/arduino/libraries/Wire -I /usr/share/arduino/libraries/Wire/utility -c -MMD -MP -MF .dep/dcmotor.cc.dep -o dcmotor.o dcmotor.cc
dcmotor.cc: In function ‘void setup()’:
dcmotor.cc:9:10: error: no match for ‘operator!’ in ‘!Serial’
dcmotor.cc:9:10: note: candidate is: operator!(bool)
make: *** [dcmotor.o] Error 1

I compile this file from command line. I use this makefile.

http://ed.am/dev/make/arduino-mk

But even if I use Arduino IDE, I get the same error message.

sketch_jul25b.cpp: In function ‘void setup()’:
sketch_jul25b.cpp:14:14: error: no match for ‘operator!’ in ‘!Serial’
sketch_jul25b.cpp:14:14: note: candidate is: operator!(bool)

When I comment

// while (! Serial);

line like this it compiles fine.

But if I enter a value between 0 and 255 motor does not turn. Instead it makes a whispering sound.

Any ideas. Thanks.

It compiles OK here using the 1.0.5 IDE with or without the !Serial(); line

The program itself is useless, but at least it compiles.

I updated IDE to the latest version 1.0.5. This time program compiles fine but still motor does not turn. I use a 2N2222A transistor instead of 2N2222.

Any ideas.

Check your wiring and post a copy of the circuit.

As written the program needs something to be entered into the serial monitor in order for the motor to be turned on, though heaven knows why. Try this simplified program.

void setup()
{
  pinMode(3, OUTPUT);
  analogWrite(3, 200);
}

void loop()
{
}

Ok. I changed the program. And here is the schematics of the circuit.

But still motor doesn't turn. It makes a 'whiiiz' sound.

Also I don't use an external power supply to power Arduino.

R1 is a 270 ohm resistor, diode is N4001 and I think the transistor is 2222A. KSP 2222A -N31 (or -B31) is written on component's casing.

You should be driving your motor from an external power supply, not the Arduino (you may damage the Arduino if you don't). Also if your transistor is not saturating you should try a power MOS FET or high gain power transistor.

If you place a HIGH on o/p D3, what is the voltage from the collector to the emitter of the 2N2222?

I changed my circuit like this. I used a 9 volts DC wall adapter to power the DC motor. I changed the resistor value to 1K and put a 1uF capacitor between VCC and transistor's collector.

LarryD: If you place a HIGH on o/p D3, what is the voltage from the collector to the emitter of the 2N2222?

I changed the program like this.

void setup()
{
  pinMode(3, OUTPUT);
  digitalWrite(3, HIGH);
}

void loop()
{
}

After uploading this program to Arduino I measured 2.28 volts from the collector to the emitter of the transistor.

But still motor does not turn and makes a whispering sound.

LarryD: Also if your transistor is not saturating you should try a power MOS FET or high gain power transistor.

May you please give some examples of high gain power transistor and power MOSFET's. (i.e. actual part names)

Also should I use BC547 instead of PN2222 transistor?

Thanks for the detailed explanations.

2.28 volts from the collector to the emitter

That is a problem.

But still motor does not turn and makes a whispering sound.

That's odd, since there is a constant i/p of 5V to the base resistor.

What do you measure on the Arduino pin 3 ?

The BC547 is much the same so it probably would work either.

TIP122 Darlington transistor http://dalincom.ru/datasheet/TIP122.pdf https://arduino-info.wikispaces.com/DC-Motors

IRF540 MOS FET http://hackerfriendly.com/wp-content/uploads/2008/12/irf540a.pdf http://www.electronics-tutorials.ws/transistor/tran_7.html

LarryD: What do you measure on the Arduino pin 3 ?

I measure 4.86 volts.

But when I plug DC wall adapter I measure voltage about 8 volts between VCC and ground. If I plug Arduino to the USB cable it drops to 2,51 volts. Isn't it odd?

Here is the actual circuit on the breadboard.

LarryD: IRF540 MOS FET

I bought an IRF540 MOSFET. Which changes should I need in the circuit to drive the DC motor using MOSFET.

Thanks.

See Attached schematic.

Finally it works! Many thanks :)

I think for the first motion it needs a lot of power. So if I enter a value below 150 still it doesn't turn. But if I enter 150 and when motors starts to move than it is possible to slow down the motor by entering smaller values.

Good for you for sticking with it!