Power / Code Inquiries with Servo and Infrared Distance Sensor

So I’m a regular newbie, who has been tackling this Photon world of Particle for a week now.

Im planning on purchasing the ‘Linker Infrared Distance Sensor for Arduino’ from my local Jaycar store, and want to use it as a trigger for a servo to hit a cymbal with its arm.

Would a 6V Standard Servo do the trick? If it is powered by a 5V wall plug it wont burn out the particle, the sensor or the servo?
Or do i need a bigger power supply so each thing has enough power?

Also, how do i go about coding the sensor so it triggers the servo movement?

Also I have attached a diagram for reference. The 5V and the GND are hanging off the top rail suggesting they are attached to a wall wort. What V/AMP should the wall wort be???

Thanks in advance!

You should definetly not have the 5V supply voltage hooked up to the 3V3 pin as your schematics show!
The Photon needs to be powered via the Vin pin when supplying with voltages between 3.6V and 5.5V
With the servo on the same rails you might also want to add some filter caps.

If the 6V servo will be happy with 5V supply should be tested :wink:

About amperage of the wall wart, the biggest load will be the servo. The Photon with 80-200mA and the sensor with 30mA should be no problem, but with the servo I’d go for a 5V/2A supply with low noise and ripple.
Also consider a less demanding servo, unless your cymbal/drumstick requires it.

So this diagram is starting to look more like a functioning, non-sizzling circuit?

Im looking to power it with a wall charger. The stats on it are;
INPUT 240V 50-60Hz 200mA
OUTPUT 5.0V 700mA

Will this be fine to plug into the power rail? If not, what are my best options? Please provide links if possible.

Thanks @ScruffR, considering I’m supposed to have this thing firing and operative by tomorrow, I appreciate this muchly.

You have a GND pin on the other side too :wink:

Seen my update above?
Something like this might be useful if you are intending to do some more breadboarding


A wall charger usually hasn’t got enough filtering to keep the Photon safe over extended periods of time.
But if your time scale is short, you could always pop in some caps between Vcc & GND.

Thanks for the hint @ScruffR, I dont mind the banter at all when you’re making progress this easy!

Okay, so you’re saying it will work, but the noise it will make will be substantial? Could I use an iphone port or something like that instead? Ive heard to not use USB as there is other wires involved and such.

Also, do you think a less demanding servo could power a drumstick to hit a cymbal to a loud enough volume? I’m just not sure what will be strong enough due to my inexperience with this.

for the sake of the thread ill upload the cleaner version of the diagram :wink:

I’m not quite with you there - when I was talking about the noise of the wall charger, I was refering to the electrical noise (unclean DC) :wink:
But iPhone chargers seem to be top when it comes to DC quality and using USB should not be a problem.
The only thing to consider, if you’d intend to power your whole system via the Photon’s USB port, is the protection diode between the USB port and Vin, which limits the maximum current Vin can source (and you’ll only get 4.8V instead of 5V).

And about the audible noise, I have no feeling what force and hitting speed you’d need or your servo will manage to deliver - also depends on the mechanics involved (e.g. levers, gears, …)

I see! well that is definitely more important I’m assuming. I have no problem with audible noise by the way.

will that be a problem in terms of performance for my sensor and servo? will they still run fine?

No problem for the sensor, but since I have no data about your servo, I can’t say.

If you try and see the Photon drop from the cloud when working the servo, then I’d say you might be better off with powering the system externally and not via Vin.
But if it doesn’t and you aren’t chiming the thing every few seconds, I’d guess you’ll be OK.

What servo size would you reccomend for this project? You suggested earlier that the one I mentioned could be downsized? If you could reccoment one that would be fantastic.

how can i power the particle if im not attached to the VIN or 3V3? do you mean via the usb?

Yup, if powered via USB, you can power light weight devices via 3V3 and/or Vin (which is then Vout ;-))

If your mechanical needs are not to great, you could go with this servo that comes with the Particle Maker Kit too
http://www.emaxmodel.com/es08a-ii.html
https://docs.particle.io/datasheets/particle-shields/#micro-servo-1-

also what did you mean by this?

Could i strip the microUSB end of the iPhone charger cable and directly plug it into my breadboard? I believe that may be the best idea for what I have on hand for the moment.

I might buy the servo you recommended individually (not in the kit)
will that be more powerful than a micro servo 9g tower pro?

@ScruffR is it possible that if I do run this directly into my breadboard, will the sensor and the servo be able to recieve 5V each from the power rail? or does it share the power and become 2.5V each? not to mention the particle…

Hi guys, I have the following set-up.

I’m looking for someone wise to tell me how on EARTH i’m supposed to code this.

My aim is to make the servo move 60 degrees downward and then back to position every time the sensor is triggered.

I’ve had a look at some other tutorials like gongbot; https://www.hackster.io/brycekahle/gongbot-515658?ref=part&ref_id=6597&offset=67

but gongbot is cloud controlled and I just want mine to be triggered by the sensor and nothing else. Can anyone whip up a code for me with the correct pins and things? I’ve never coded before and this makes no sense to me.

That’s one of the basic rules in electrics:
Loads in parallel will always “see” the same voltage, loads in series will always have the same current flow through them.

So yes, you should have each of your three (or two, if the Photon is the “source”) components connected to the rails.

About the coding, we do prefer to see your code first and provide advise than just providing code :wink:

But the magic words to look at in the docs would be analogRead() for the sensor and Servo for the … you guessed it :wink:

Your sensor will give you a voltage reading between 0.4V and 3.1V - depending on distance (see datasheet chart)

int sensor = A0;
Servo myservo;

void setup () {
  pinMode(sensor, INPUT);
  myservo.attach(D0);
}

void loop (){
  if (analogRead(sensor) == HIGH) { 
    myservo.write(90);
    delay(200);
  }
  if (analogRead(sensor) == LOW) {
    myservo.write(0);
    delay(200);
  }
}

this is my code currently. make of it what you will! some parts i understand some i dont. its been mixmatched from tutorials

analogRead() will report a value between 0 (=0.0V) and 4095 (=3.3V), while HIGH = 1 and LOW = 0
So there you’d need to do some value mapping to have your returned value correspond to the output voltages of your sensor (see chart).

https://docs.particle.io/reference/firmware/photon/#analogread-adc-

Try this instead (it’s not your solution, but should make you see what’s going on)

int sensor = A0;
Servo myservo;

void setup () {
  //pinMode(sensor, INPUT);                  // don't do this for analogRead() - see link
  myservo.attach(D0);
}

void loop () {
  int reading = analogRead(sensor);          // get the ADC reading
  int voltage = 3300 * reading / 4095;       // convert to mV
  int angle = map(voltage, 0, 3300, 0, 270); // convert full voltage range to 0° ~ 270°

  myservo.write(angle);                      // set the servo
  delay(1000);                               // only once per sec to start with
}

thanks @ScruffR, it does begin to make sense.

Unfortunately I went to pick up the distance infrared and all they had was a PIR Sensor. This i sensor has the classic 12V input and ground, which i’ve connected via this product http://www.minikits.com.au/LM2596-PSU-01

Two questions. Because I am powering it up to 12V from 5V, do I go Input pins with 5V to 12V output or do i choose output pins wiith 5V and input pins with 12V?

And with the sensor, it has 6 pins. 12V, GND, (both which i have picked) and the other four are two ZONE, and two TAMPER, which one do i connect my analogreading pin to???

Thanks in advance. Ill be working on this and the code all through the day so all help is appreciated.

If you are asking about a specific sensor it would be good to look at and provide a link to the sensor data sheet.
The code above is targeted at the other sensor and most likely will not work with this PIR sensor and most importantly if your sensor has a 12V output you’ll fry your Photon too.

I’d rather say you order another better suited sensor than messing with half fitting bits and bobs and workarounds.

Hi @ScruffR, Ive decided now for certain how the project is going to work.

The sensor is the the IR Obstacle Avoidance Sensor Module, powered at 5V.
I have the Particle Photon, powered at 5V
And a Arduino Audio FX Soundboard.

would you be able to teach me more about value mapping? I still have your pseudocode in my sensor code.

This is the current code for the above equipment. I know i have swayed from the servo, but this is due to buying equipment that suited the project more like you suggested!

If your sensor is similar to the one featured on this site, you can just take the code from there and adjust the pin mapping to your needs
http://henrysbench.capnfatz.com/henrys-bench/arduino-sensors-and-input/arduino-ir-obstacle-sensor-tutorial-and-manual/

Do you mean the Adafruit Audio FX Soundboard?
https://www.adafruit.com/products/2133

It will certainly do the job, but seems a bit over-sized. Unless you need the recording and multi format capabilities.
Otherwise, if you only want audio playback, you could always go with this little chap here
http://de.aliexpress.com/item/Mini-MP3-Player-Module-with-Simplified-Output-Speaker-for-Arduino-UNO/32355786978.html?spm=2114.47010508.4.2.EKYvGS

And I’ve worked with that one, while I was intending to look into the other one too, but haven’t had the hardware yet.

BTW: The code I provided above, is no pseudo but working code, it just didn’t do exactly what you outlined above.
The value mapping shown in there is the way (or at least one of them) to do it.
The map() function used is nothing else than a linear translation from one k0*x+d0 into another k1*x+d1 system.
See here
https://www.arduino.cc/en/Reference/Map

1 Like