Congratulations Rich, for answering the question and has received an EZ-Credit award! Share what you have learned and create a tutorial to help others by clicking here.

Trying To Get A Pir Working

Assistance Requested

Help bhouston with their question and receive $10 of EZ-Credit to get more robots and parts from our store. The following information was provided about their previous efforts searching tutorials for a resolution.

bhouston claims to have checked these sources:

I feel like a blind man wandering around in the bush at night hoping to find my way home!

So here's where I'm at with this project, I've put some comments in so you can see what I'm trying to accomplish. Any and all help will be appreciated.


#timer to power down
$x = 0
RepeatUntil($x = 30)


#what to do when timer gets to 30

#$x = GetDigital(D2)
ControlCommand("Script Manager", ScriptStart, "power down")#do this once


#do this when the PIR is triggered after "power down"

IF($motion=1) #pin went high
ControlCommand("Script Manager", ScriptStart, "Power up")#do this once

#do this if PIR is triggered anytime after it has powered up

#stay powered up - need something here - what?

# if Pir is not triggered in 30 sec start the timer again

If($x = 30)


PS. I would pay money to go to a course somewhere to learn EZ-Robot scripting!

I have found a few conversations, tutorials and activities that may help with your question. Take a look at these links. I've sorted them by what I believe to be most relevant but that is not always the case as I'm still learning.

Also, consider reviewing the Learn section for informative lessons and activities. Check it out!


No need to pay money dude, I'm usually around to answer pretty quickly on here and if I'm not Richard R is getting the hang of things too and seem to be on here as much as me these days Smile

To be honest I'd prefer (well, love to actually) rewrite this one (including comments and explanations to help you understand it all) however that'll have to wait until I finish work but to get it straight can you confirm the operation and order of.

Presumably it's this;

Robot sleeps until awoken by PIR sensor?

Robot powered up manually first time.
Robot detects movement via PIR
After 30 seconds of inactivity robot moves to low power state while still checking for movement
On detection of movement robot wakes up
After 30 seconds of inactivity robot moves to low power state while still checking for movement
and so on

I'll knock something up quickly later.


Yes Rich, you have the jest of it. I'm away for the weekend so no rush. Going to Calgary -maybe I'll stop in at the mothership and say Hi.
Thanks for your help


I realised while writing this that I haven't written much for a while so it may be a little rusty or there may be some commands which can be written better but here is an example to get you going.

All scripts I assumed would be in "Script Manager", if they are separate scripts or your rename Script Manager you need to check the ControlCommands()

This will use three scripts. One for monitoring of the PIR over a period of roughly 30 seconds. One will shut down to low power mode and monitor the PIR waiting for motion and the last will Power Up the robot and start the monitoring.

Until all three scripts are written/added you may get some errors if you try to run them as the scripts will not exist.

First start with the power up script.


# Place holder for power up
# Enter power up commands here

# Start monitoring
ControlCommand("Script Manager", ScriptStart, "Monitor")

This will turn on all functions you need to have turned on and then start the "Monitor" script in Script Manager. It's this script which you would want in your "init" script which is run on connection.

It's a very basic script which shouldn't need any more explanation than this.

Next we need the "Monitor" script.


# Monitor for movement for 30 seconds

# Set a lable for the start

# Reset the counter
$Monitor_Counter = 0

# Repeat
RepeatUntil($Monitor_Counter = 30)

# Increase the counter by 1

# Check for detection on PIR - since we need to check 5 times per second use repeat

# checking counter needed
$Monitor_Check_Counter = 0

RepeatUntil($Monitor_Check_Counter = 5)
# Add 1 to the check counter

# Check the status of the digital port
$PIR = GetDigital(D1)
# Check if it is high or low and take action
IF($PIR = 1)
# Motion detected, return to start
# Note - no else or elseif required since nothing happens if motion isnt detected

# Wait a while to save system resources

# 1 second has passed by this point
# repeat 30 times

# 30 seconds passed - Enter low power plus monitoring mode
ControlCommand("Script Manager", ScriptStart, "LowPowerMonitoring")

# End the script

The comments in the script should explain each part of it.
The HALT() at the end is optional. Without it the script will just stop anyway however I have included it as I like to use HALT() and it also should indicate where the script will end.
When motion is detected the script will jump back up to the start rather than continue in it's current Repeat loop. Next time the Repeat loops are run the counters are reset to 0 so the time is reset to 0.

When the time runs out without motion detected the script starts up the LowPowerMonitoring script and this one ends.

So now, all that is left is the low power monitoring script;


# Enter your low power mode commands here

# Once in low power mode wait for a change on the PIR digital port

# Power up robot
ControlCommand("Script Manager", ScriptStart, "PowerUp")

# And end this script

Another very simple script. The start needs editing for your low power commands, ControlCommand()s to shut off or pause functions etc.

Then it sits there monitoring the digital port (D0 in this case, edit to suit). When it changes, i.e. when there is motion it uses ControlCommand to start up the Power Up script again. The power up script starts the monitoring script and we have an endless circle.

Hopefully that should get you going Smile


Thanks Rich, I'll run it when I get back. Just dropped in at EZ-Robot here in Calgary, nice bunch of people!


@Rich, Thanks for the code it works great! Now I have to figure out how to actually "power down" and "power up" the servos.

Thanks again,


Power down is easy



Presuming it's on board 0. If on another board change the 0. If multiple boards put each board on a new line i.e.



If you only want to power down a specific servo use



Or multiple servos



Or multiple servos across multiple boards




To further what @Rich said... Once you release your servos you will have to reset servo speed if you are using anything other than full speed....


Thanks, makes sense. So now I have been playing around and I switched the PIR input port from "D2" to "ADC7". I have changed the references to the port in the script, now when to script runs, it skips the "LowPowerMonitoring" and goes directly the "Power Up" script.
It worked fine when using "D2", I changed ports because all of my digital ports are in use.


@bhouston... Digital will return a value of 1 or 0 (true or false)... whereas 8 bit analog will be something like 0 - 255... So when reading analog you should assume the PIR has been activated if the adc7 value is greater than say something like 100... The number may vary so you will need to test this...