Author Topic: Art Project in danger: Servo and Led do not work together  (Read 14713 times)

nero

  • Newbie
  • *
  • Posts: 12
Art Project in danger: Servo and Led do not work together
« on: February 19, 2013, 08:09:49 am »
Hello, we are constructing an art installation with 50 digisparks and have a problem.
We added an extra row of ground pins side by side to the normal pins and connected a vibration sensor to pin5, an extra led to pin 0 (digispark model B) and a servo to pin1. We use the servo sketch from Eje_G because the servo lib did not work.

The vibration sensor works fine, no problem with that.
When we add the Led everything works fine too, it glows when someone touches the sensor.
When we add the servo line everything works fine too, it moves when someone touches the sensor.

When we add the Led and the servo...nothing works any more!
The strange thing is, the onboard Led works fine only the additional Led causes the problem but it is on the same pin!

We tried:
- an other pin for the Led > same problem
- an other pin for the servo > same problem
- use digitalWrite instead of analogwrite for the Led > same problem
- put a resistor to the Led > > same problem

Any ideas?

Layout:


Code: [Select]
// Mimose

int i=0;
int wert = 0;


void setup() {
 
 
  pinMode(0, OUTPUT); // onboard LED (Model B)
  pinMode(1, OUTPUT); // Servo
  pinMode(2, OUTPUT); // Neuronpin OUT
  pinMode(3, INPUT);  // Neuronpin IN
 

 
  analogWrite(0, 0);
  digitalWrite(1,LOW);
  digitalWrite(2,LOW);
     
  // Servo in Startposition   
  for(int startpos = 0; startpos < 400; startpos +=1)
   {
   digitalWrite(1,HIGH);
   delayMicroseconds(1400);
   digitalWrite(1,LOW);
   delayMicroseconds(1400);
   delay(20);                    // wofür?? 
   }
   
 
   
   
}

// loop --------------------------------------------------------------------------------

void loop() {
// 00 - Reset Led
      analogWrite (0, 10);
 
 
// 01 - Sensor auslesen und filtern
      i = analogRead (0);
      wert = (1/i)*500;
     
// 02 - handeln wenn Fühler aktiv
      // wenn Fühler aktiviert - irgendein Schwellenwert
      if (i >= 1020){
       
     
        /* 02a - Neuron Sig. senden
        digitalWrite (2, HIGH);
        delay (10);
        digitalWrite (2, LOW);
        */
       
        // 02b - Led Funktion aufrufen
        //ledFunktion (200); // Led hell
     
        // 02c - Servo Funktion aufrufen
        servoFunktion (2400); // maximal fahren
       
      } // Ende if
     
     
/* 03 - handeln wenn Fühler inaktiv
      if (digitalRead (3) == HIGH){
       
       
        // 03a - Neuron Sig. senden
        digitalWrite (2, HIGH);
        delay (10);
        digitalWrite (2, LOW);
       
       
        // 03b - Led Funktion aufrufen
        ledFunktion (10); // Led weniger hell
       
     
        // 03c - Servo Funktion aufrufen
        servoFunktion (1200); // halb fahren
       
      }
     
     
      else {
        // 04a - Led Funktion aufrufen
        ledFunktion (0); // Led aus
     
        // 04b - Servo Funktion aufrufen
        servoFunktion (400); // Servo auf Nullpos.
      }
*/
 
} // End loop



// Funktionen ----------------------------------------------------------------------------
 
  // Led schreiben
  void ledFunktion (int ledWert) {
      analogWrite(0, ledWert);   // turn the LED on (HIGH is the voltage level)
      delay (100);
      analogWrite(0, 0);
  }
 
 
  // Servo fahren bis zum übergebenen Wert, bisher nur oberer Wert, kann aber auch oben und unten sein
  void servoFunktion (int servoWert) {
   
    // Led an solange Servo fährt - Led Funktion unnötig
       analogWrite (0, 200);
   
      // sweep 1 counter clockwise
       for(int pos = 400; pos < servoWert; pos +=30)
       {

         digitalWrite(1,HIGH);
         delayMicroseconds(pos);
         digitalWrite(1,LOW);
         delayMicroseconds(pos);
         delay(15);

       }

             delay (100); // Pause

      // sweep 1 clock wise
       for(int pos = servoWert; pos > 400; pos -=30)
       {

         digitalWrite(1,HIGH);
         delayMicroseconds(pos);
         digitalWrite(1,LOW);
         delayMicroseconds(pos);
         delay(15);

        }
       
        analogWrite (0, 10);
       
        delay (200); // nur in Test
  }


DeuxVis

  • Full Member
  • ***
  • Posts: 107
Re: Art Project in danger: Servo and Led do not work together
« Reply #1 on: February 20, 2013, 02:00:57 am »
First, It's not clear on your schematic if the servo signal output is connected back to ground. I suppose it is, otherwise the signal wouldn't mean anything.


Second, your schematic show the additional led plugged to P0 without a serial resistor, it might draw too much current if that's an accurate description of your circuit. That may be a possible cause to the reboot (you might also fry the led depending on how much current it can handle).

Third, I'm pretty new with all that electronics stuff, so take some other advices (while in the mean time you could try my suggestions if that made any sense to you).

What do you use to give power to the digispark ?


nero

  • Newbie
  • *
  • Posts: 12
Re: Art Project in danger: Servo and Led do not work together
« Reply #2 on: February 20, 2013, 08:00:14 am »
Hello DeuxVis,

we tested the led with resistor too but it made no defference.
The servo signal output is not connected to ground but that is right and the servo works like this.


We found out that the servo signal is not working when the led is on.
We worked around the problem by switching the led on and of in between the servo pulses of the software pwm.
Not a nice solution and we can not adjust the brightness of the led but it works.

We would be still very grateful if somebody tells us why it is a problem to have the led switched on while driving the servo with the software pwm.

New sketch:

Code: [Select]
int i=0;
int wert = 0;


void setup() {
 
 
  pinMode(0, OUTPUT); // onboard LED (Model B)
  pinMode(1, OUTPUT); // PWM Servo
 

 
  analogWrite(0,LOW);
  digitalWrite(1,LOW);

     
  // Servo in Startposition   
  for(int startpos = 0; startpos < 400; startpos +=1)
   {
   digitalWrite(1,HIGH);
   delayMicroseconds(1400);
   digitalWrite(1,LOW);
   delayMicroseconds(1400);
   delay(20);                   
   }
   
 
   
   
}

// loop --------------------------------------------------------------------------------

void loop() {
// 00 - Reset Led
      analogWrite (0, 0);
 
 
// 01 - read sensor output and filter it
      i = analogRead (0);
      wert = (1/i)*500;
     
// 02 - do something if feeler is activated
     
      if (i >= 1020){
       
        // 02c - call servo function

        servoFunktion (2400); // drive to max. position
       
      } // Ende if
     

 
} // End loop



// Functions ----------------------------------------------------------------------------

 
 
  // drive servo to given position "servoWert"
 
 void servoFunktion (int servoWert) {
   
    // software pwm

      // sweep 1 counter clockwise
       for(int pos = 400; pos < servoWert; pos +=30)
       {

          digitalWrite (1, HIGH);
                digitalWrite (0, HIGH); // Led on
                digitalWrite (0, LOW);  // Led off
         delayMicroseconds(pos);
         digitalWrite(1,LOW);
                digitalWrite (0, HIGH); // Led on
                digitalWrite (0, LOW); // Led off
         delayMicroseconds(pos);
         delay(15);

       }

             delay (200); // break

      // sweep 1 clock wise
       for(int pos = servoWert; pos > 400; pos -=30)
       {

         digitalWrite(1,HIGH);
                 digitalWrite (0, HIGH); // Led on
                 digitalWrite (0, LOW);  // Led off
         delayMicroseconds(pos);
         digitalWrite(1,LOW);
                 digitalWrite (0, HIGH); // Led on
                digitalWrite (0, LOW);   // Led off
         delayMicroseconds(pos);
         delay(15);

        }
       

       
        delay (200);

         
  }

RussNelson

  • Newbie
  • *
  • Posts: 23
Re: Art Project in danger: Servo and Led do not work together
« Reply #3 on: February 20, 2013, 08:35:05 am »
You should always always have a resistor on an LED. They don't operate on voltage, they operate on current! The only reason you're able to get away with connecting it straight up to an output pin is that the DigiSpark runs at 3.3V, which is close to the forward voltage drop of modern high-intensity LEDs, and because the outputs are limited in how much current they put out. You should put a 200 ohm resistor in series with the LED. Either side -- doesn't matter. That's just as a rule of thumb -- there are actual calculations you can do to ensure that you get the maximum brightness out of the LED without overdriving it and limiting its brightness.


Don't know why you're having trouble outputting to both pins, but it's possible that the LED is pulling the voltage down on other pins. It's likely that the servo requires a "real" 5V logic one. The digispark is going to be marginal anyway, and pulling down its voltage isn't going to help you.

nero

  • Newbie
  • *
  • Posts: 12
Re: Art Project in danger: Servo and Led do not work together
« Reply #4 on: February 20, 2013, 09:21:28 am »
Yes I know and we tested it with the resistor and we usually use resistors with leds but that did not solve the problem and because we have 50 units we try to save work wherever possible and that is why we did not add the resistors yet - may be we will do that later.

We used an oscilloscope to see if we get a drop in voltage or some backfire from the servo - but nothing.
We tried it on different pins and strange enough it all works well if we use the onboard led but gets crazy when we add another on the same pin.

digistump

  • Administrator
  • Hero Member
  • *****
  • Posts: 1465
Re: Art Project in danger: Servo and Led do not work together
« Reply #5 on: February 20, 2013, 09:54:47 am »
A few notes-


I'll give this a try later today but wanted to correct/agree with a few things


LED should ALWAYS have the resistor


The Digispark runs at 5v not 3.3v


So with it running at 5v the resistor is mandatory and running without it could cause issues with the chip


I'll post back as soon as I try this out with a servo and LED.

RussNelson

  • Newbie
  • *
  • Posts: 23
Re: Art Project in danger: Servo and Led do not work together
« Reply #6 on: February 20, 2013, 10:59:35 am »
5V?? Oh, to get the maximum clock speed, right. But then, the LED pin is only pulling up to 3.7V driving only the LED. That implies that its barely able to drive one LED.


Nero, have you tried cutting the trace that drives the onboard LED?

nero

  • Newbie
  • *
  • Posts: 12
Re: Art Project in danger: Servo and Led do not work together
« Reply #7 on: February 20, 2013, 11:08:35 am »
We tried the led on different pins with the same result - on the other pins the onboard led was off.
The led runs stabil on every pin, with or without resistor.

We use a not very powerful yellow led, second lowest we could get, 700microcandela.
We also tried a standart red and a white led, same result, servo is not working.

Sergio

  • Newbie
  • *
  • Posts: 9
Re: Art Project in danger: Servo and Led do not work together
« Reply #8 on: February 20, 2013, 01:03:04 pm »
Where is the Vcc for the servo coming from?

nero

  • Newbie
  • *
  • Posts: 12
Re: Art Project in danger: Servo and Led do not work together
« Reply #9 on: February 20, 2013, 01:44:54 pm »
The servo and the Digisparks are connected to the same regulated 4,5V power supply.
We checked the network an could measure 4,61V to 4,64V at all points.

digistump

  • Administrator
  • Hero Member
  • *****
  • Posts: 1465
Re: Art Project in danger: Servo and Led do not work together
« Reply #10 on: February 20, 2013, 04:47:04 pm »
nero


I've tested the following code - and it works perfectly for me:


Code: [Select]



// Mimose


int i=0;
int wert = 0;


void setup() {
 
  pinMode(0, OUTPUT); // onboard LED (Model B)
  pinMode(1, OUTPUT); // Servo
  pinMode(2, OUTPUT); // Neuronpin OUT
  pinMode(3, INPUT);  // Neuronpin IN
 
  analogWrite(0, 0);
  digitalWrite(1,LOW);
  digitalWrite(2,LOW);
     
  // Servo in Startposition   
  for(int startpos = 0; startpos < 400; startpos +=1)
   {
   digitalWrite(1,HIGH);
   delayMicroseconds(1400);
   digitalWrite(1,LOW);
   delayMicroseconds(1400);
   delay(20);                    // wofür?? 
   }
     
}


// loop --------------------------------------------------------------------------------


void loop() {
// 00 - Reset Led
analogWrite (0, 10);
 
servoFunktion (2400); // maximal fahren
       
delay(1000);
     
     
 
} // End loop






// Funktionen ----------------------------------------------------------------------------
 
  // Led schreiben
  void ledFunktion (int ledWert) {
      analogWrite(0, ledWert);   // turn the LED on (HIGH is the voltage level)
      delay (100);
      analogWrite(0, 0);
  }
 
 
  // Servo fahren bis zum übergebenen Wert, bisher nur oberer Wert, kann aber auch oben und unten sein
  void servoFunktion (int servoWert) {
   
    // Led an solange Servo fährt - Led Funktion unnötig
       analogWrite (0, 200);
   
      // sweep 1 counter clockwise
       for(int pos = 400; pos < servoWert; pos +=30)
       {


         digitalWrite(1,HIGH);
         delayMicroseconds(pos);
         digitalWrite(1,LOW);
         delayMicroseconds(pos);
         delay(15);


       }


             delay (100); // Pause


      // sweep 1 clock wise
       for(int pos = servoWert; pos > 400; pos -=30)
       {


         digitalWrite(1,HIGH);
         delayMicroseconds(pos);
         digitalWrite(1,LOW);
         delayMicroseconds(pos);
         delay(15);


        }
       
        analogWrite (0, 10);
       
        delay (200); // nur in Test
  }









With unmodified Model A and Model B Digisparks. I have the servo connected to P1 and an LED connected with a resistor inline to P0. The resistor on the LED is absolutely necessary and cannot be omitted without risking greatly shortening the life of the Digispark and producing unpredictable results. I used the USB connector to power it with 5 volts - though any 5v supply should achieve the same results.


Bluebie

  • Sr. Member
  • ****
  • Posts: 486
Re: Art Project in danger: Servo and Led do not work together
« Reply #11 on: February 20, 2013, 08:24:15 pm »
What do you mean the servo isn't connected to ground? You cannot just connect one data wire from the digispark to the servo's control input and then power it seperately. You need to connect the servo's power supply ground to the digispark's ground, or you need to use something like an optical isolator or capacitors or a transformer if you can't connect the ground's together for some reason to remove the DC component while still connecting the grounds together. If the ground really isn't connected, you are transferring data to the servo capacitively through the air, or possibly through some common ground you aren't aware of. It's surprising that it ever works! It is not a problem that it sometimes doesn't!

Mark

  • Full Member
  • ***
  • Posts: 196
Re: Art Project in danger: Servo and Led do not work together
« Reply #12 on: February 20, 2013, 08:57:46 pm »
I agree with Bluebie....

Perhaps it would help if you provided the complete schematic, as it appears there may be some connections missing.

Regarding LEDs YOU NEED TO ADD A SERIES RESISTOR !!

You cannot and should not rely on the internal current limiting of any chip to do this.
I am tired of seeing demonstrations on the net where the series resistor is omitted...it teaches very bad habits.

The 3.5v or even 2v is enough to drive a led, as they need 10-20mA flowing through them to light up. The resistor is sized to ensure this limit is achieved.

I am wondering if the second code you posted is correct.
Code: [Select]
          digitalWrite (1, HIGH);
                digitalWrite (0, HIGH); // Led on
                digitalWrite (0, LOW);  // Led off
         delayMicroseconds(pos);

It seems to me that turn ON Pin 1 (servo), then turn ON Pin 0 (led) and turn it OFF immediately.
You then delay for 400 - 1200 Micrsecs, and turn OFF Pin 1 (servo) and repeat the led on/off.
You then have a delay of 15mS and continue incrementing the loop by 30, and repeating.

To me this seems to explain why you can't adjust the brightness, and I'm surprised the Led actually lights at all.

By contrast your first code turned on the led, did the servo movement, then turned off the led ...much as Erik has duplicated.


Mark


bjh

  • Newbie
  • *
  • Posts: 31
Re: Art Project in danger: Servo and Led do not work together
« Reply #13 on: February 20, 2013, 11:01:26 pm »
BTW, I've just now posted a new feature-complete (but minimally tested) version of my SimpleServo library inspired by Eje_G's sketch.  Check it out, it could simplify your code, and let me know what you think!


--bjh

nero

  • Newbie
  • *
  • Posts: 12
Re: Art Project in danger: Servo and Led do not work together
« Reply #14 on: February 21, 2013, 12:15:41 am »
As I wrote both, the servo adn the arduino are connected to the same power source and so they are both connected to the same ground. So that can not be the cause of our problem.
We will try another power source maybe our transformators ground is not big enough.

The Led will be connected with a reistor ok  ok - but we tried this and we still had the same problem.

mcb1: This was a "work around" to get the Led on and the servos going at the same time beause we still can not drive the servo while our led is on so we are switching them on and off one after the other in the software pwm, we know that we can not control the brightness like this.

If it worked when "digistump" tried it, it seems we must have a problem caused by our servos or power supply.