Go Down

Topic: Trouble with adding one program line (Read 591 times) previous topic - next topic


Recently purchased an UNO R3 (1.8.5) and have been working through the Starterkit_Basickit. All was well until I decided to add one program line to the MotorizedPinwheel sketch. I want an LED to light along with the motor running. When I add pinMode(5,OUTPUT); under the existing two pinMode statements the sketch compiles but after downloading the L3 flashes quickly (sets of 3 flashes) and the sketch doesn't run. When I remove the line all is well again. On a couple of occurences, after waiting minutes, the problem resolved itself and L3 remained steadily lit. Confused! Any assitance would be appreciated.


Please post your full sketch. If possible you should always post code directly in the forum thread as text using code tags (</> button on the toolbar). This will make it easy for anyone to look at it, which will increase the likelihood of you getting help. If the sketch is longer than the forum will allow then it's ok to add it as an attachment.

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you're using the Arduino Web Editor then you will not  have access to this useful tool. I recommend using the standard Arduino IDE instead.

When your code requires a library that's not included with the Arduino IDE please always post a link(using the chain link icon on the toolbar to make it clickable) to where you downloaded that library from or if you installed it using Library Manger(Sketch > Include Library > Manage Libraries) then say so and state the full name of the library.


Thanks for the advice. Will familiarize with Tools>Auto Format and will follow up with a post containing the code.


Here is the suspect code. I have added the pinMode OUTPUT and digitalWrite commands to light an LED on pin 5 when the motor runs. The new commands cause my UNO to enter a fit where "L" led flashes quickly three times repeatedly. If I remove the new commands all is good again. Very confused.

Code: [Select]

  Arduino Starter Kit example
  Project 9 - Motorized Pinwheel

  This sketch is written to accompany Project 9 in the Arduino Starter Kit

  Parts required:
  - 10 kilohm resistor
  - pushbutton
  - motor
  - 9V battery
  - 1N4007 diode

  created 13 Sep 2012
  by Scott Fitzgerald


  This example code is part of the public domain.

// named constants for the switch and motor pins
const int switchPin = 2; // the number of the switch pin
const int motorPin =  9; // the number of the motor pin

int switchState = 0;  // variable for reading the switch's status

void setup() {
  // initialize the motor pin as an output:
  pinMode(motorPin, OUTPUT);
  // initialize the switch pin as an input:
  pinMode(switchPin, INPUT);
  // initialize the LED pin #5 as an output:
  pinMode(5, OUTPUT);

void loop() {
  // read the state of the switch value:
  switchState = digitalRead(switchPin);

  // check if the switch is pressed.
  if (switchState == HIGH) {
    // turn motor on:
    digitalWrite(motorPin, HIGH);
    // and turn LED on:
    digitalWrite(5, HIGH);
  } else {
    // turn motor off:
    digitalWrite(motorPin, LOW);
    // turn LED off:
    digitalWrite(5, LOW);


Pin 5 should have been defined as a "named constant" but otherwise this code looks good.  What version of the IDE is being used?


Dec 14, 2017, 02:32 am Last Edit: Dec 14, 2017, 02:33 am by MarkT
How are you powering the motor?

What current limiting resistor are you using on the LED on pin 5?
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]


I'm using IDE 1.8.5. Powering the motor via 9V and a MOSFET per the circuit drawing in Exercise #9 of Project Book (StarterKit_BasicKit). Limiting current through the additional LED via the typical arrangement of a 220 OMH resistor in series between the LED cathode and ground. Example Sketch and circuit work fine until I add the commands for the additional LED. Was really hoping the repeating three flashes of LED L would be of meaning (error code)? Starting to think something is flakey with my Arduino and not the code as a few times after loading the modified sketch (many minutes later) the flashing stops and the sketch works, other times the flashing continues indefinitely.


Of course, I do not have the same hardware (motor, switch, power supply, etc.) or IDE version, but it works just fine on my Uno using IDE version 1.8.1.  I changed the 5 to a 13 so it would work using the onboard LED.

I would try another Uno, or try removing the IDE and installing the same or a different version.  I am sorry that you are having this issue.  It should be easy and straightforward.


Thanks I'll try both options. Am starting to think I'm jinxed as it was going so smoothly and now this simple little bug is really bugging me. My goal is to place a mini in a family heirloom snow globe that has a blower motor and led lighting. The existing motor and PCB are shot and I was really optimistic that the Arduino would allow me to control the blower motor and lighting exactly as required.


Something went wrong but I do not believe that you are jinxed.

By the way, it is usually not a good idea to power a motor from the Arduino (control, yes, but not to provide power), and a common 9-volt battery does not power most motors for very long.

Good Luck!


Tried IDE 1.8.1 but am having the same results, a flashing L led. Also should reiterate the 3-12V motor is being powered by a 9V battery, and controlled by the arduino through a MOSFET.

I no longer think it is a software problem. I think the IC is bad. I noticed that when any of my sketches work (Starter Kit sketches) the L led stays lit. If the sketch doesn't work L is flashing three times repeatedly. Yet in none of the sketches is this LED or pin 13 used.

Read the voltage between ground and pin 13 when the L is constantly lit and see only millivolts. Once I put the meter on the leads L turns off, remove the meter from the leads and L lites up again. Confused! Going to try to get a new IC from the vendor, or worse buy a new Arduino and start over. Discouraged.

Go Up