Welcome to Synthiam!

Program robots using technologies created from industry experts. ARC is our free-to-use robot programming software that makes features like vision recognition, navigation and artificial intelligence easy.

Get Started
Asked — Edited

Led Fade

I am trying to get the PWM going to fade an LED on my IOTiny, but it doesnt seem to be working.
I have an IOTiny with 2 servos and camera. I am trying to get an LED to fade on and then off (repeatedly). I will expand to a couple more LED's once I can get this sorted.
I have tried the example set forward in tutorial:
https://www.ez-robot.com/Tutorials/UserTutorials/179/1
I also watched the video on the youtube channel:
https://www.youtube.com/watch?v=kY8iJ84J4GE

I believe the code is setting a threshold to achieve (either 0 or 100) and using the servo Speed to determine how quickly it achieves this value. When I watch the applets running the code from a script applet, the PWM just jumps between 0 and 100 (with the 3000ms delay in between). It doesnt graduate.

I set up a script to run the PWMRandom on the port, and while I can see the value changing on the PWM applet, there is no difference at the LED.
I measured the output with my analog voltmeter, and my digital meter and it seems to just output ~3v as soon as any positive change to the PWM setting occurs regardless of the value. I was expecting to see my analog meter gradually go up and down. The DMM is autoranging, and probably wouldnt pick up the change and report it effectively. I would anticipate if the value of the PWM applet is low, say 10% the LED would be dim. While 100 would be bright.

I also believe that the PWM applet would be quickly switching the voltage creating a duty cycle that would effectively lower/raise the voltage of the port.

I am powering the IOTiny with a 5v MeanWell power supply rated at 3AMPS. While the documentation states a preference for battery, and a 5Amp requirement, my meter indicates the system only draws 0.18A at idle and even with the camera it never went over 0.4A, so I believe I am within the functionality requirements of the device.
To support that position the camera is setup to perform face tracking with servo control and it works fine.

Am I wrong in my understanding?
Am i trying to implement PWM incorrectly?

The PWMRandom script is simply:

Code:


:Loop
PWMRandom( D4, 0, 60 )
goto(Loop)


where as the other script is simply the reference one:

Code:


# set the servo speed to 2
ServoSpeed(d4, 5)

:loop

# Set LED On
pwm(d4, 100)

# wait some time for the pwm to do its ramping thing
sleep(3000)

#set LED Off
pwm(d4, 0)

# wait some time for the pwm to do its ramping thing
sleep(3000)

goto(loop)

AI Support Bot
Related Content
Synthiam
#1  
I've never done this but I think you get the fade speed by setting the servospeed at different values, not the PWM. The brightness of the led would be determined by the PWM. My guess is that the led won't start showing a visible glow until there is a fast enough PWM pulse being fed it. Motors being fed with a pwm pulse wont start moving until the pwm is rather high. Even the smallest of my motors won't move unless ARC's PWM setting is above 15%. larger ones won't move until PWM is above 50% or more. I know we're talking a diode here but the concept is the same.

Are you running both these looping scripts at the same time? If so, don't. Running both on the same port may screw things up. You can set the random effect in just one looping script.

I'd still let the led go totally on with a 100% pwm and totally off with a 0% PWM. If you want random fade speed you can try the ServoSpeedRandom command but place it inside the loop. Example: ServoSpeedRandom(D4, 2, 20) . You may be disappointed because the random effect may be erratic or not even noticeable sometimes. You also need to set the sleep commands high enough to let the longest fade period happen so the led can get to full brightness.

Also try inserting 500ms sleep command after you set the servospeed just incase you're script is going too fast and not setting the servospeed before the led is switched on. You can decrease the timing if this works until it doesn't.

Also double check your wiring and to see you have everything attached to the correct ports that you have set up in ARC.

Code:


:loop

# set the servo speed to a random value
ServoSpeedRandom(D4, 2, 20)

sleep(500)

# Set LED On
pwm(d4, 100)

# wait some time for the pwm to do its ramping thing
sleep(3000)

#set LED Off
pwm(d4, 0)

# wait some time for the pwm to do its ramping thing
sleep(3000)


goto(loop)
#2  
"you get the fade speed by setting the servospeed at different values"
I think you are right there. ServoSpeed seems to be how quickly it changes the PWM value.

"Are you running both these looping scripts at the same time?"
No, they are seperate examples to try and get this to fade at all.

I will try the random servo speed, but idealy i am just look for a simple pulse/fade on-off.

The wiring is correct. Cathode to GND, Anode to SIG (D4 in my case, though i have tried other ports such as D7). No resistor, because I think the signal peaks at 3.3v.
#3  
I use PWM only on mine (not servo speed).

Have you tested with a PWM object before trying scripting just to see that the LED responds?

I think the issue with your script may be the spaces between the commas and the values. ARC is very strict on correct formatting.

Here is a script that I use to have an LED change brightness along with sound being played (using the Sound servo V4 object)

Code:


$Soundv4Value = 0
:loop
waitforchange($Soundv4Value)
PWM(D11,$Soundv4Value)
goto(loop)


Alan
PRO
USA
#4  
Does not work for me.

I waited 10 seconds after each PWMs and no Fade (In or Out).

Code:


#start off
Set(D0, 0)

#set the servo to slowest speed
ServoSpeed(D0, 10)

:loop
# Set LED On
pwm(D0, 100)

# wait some time for the pwm to do its ramping thing (Fade In)
sleep(10000)

#set LED Off
pwm(D0, 0)

# wait some time for the pwm to do its ramping thing (Fade Out)
sleep(10000)

goto(loop)


@DJ:
Can you advise ?
PRO
USA
#5  
Fade In / Out (without ServoSpeed):

Code:


#start off
Set(D0, 0)

:loop

#fade in
repeat($v, 0, 100, 5)
pwm(D0, $v)
Sleep(200)
endrepeat

#fade out
repeat($v, 0, 100, 5)
pwm(D0, 100-$v)
Sleep(200)
endrepeat

goto(loop)


Note: This is resource intensive.
#6  
I will try this as soon as I get home.
PRO
Synthiam
#7  
Use a faster servo speed rather than 10. You saw in the video how slow the 4 speed is, so you can imagine how slow 10 is - just use a faster value in your project.

Oh - and according to the documentation for the servoSpeed() command, you are required to initiate a pwm position first. So set a position before executing the command.
PRO
USA
#8  
@DJ:
Still not working.

Code updated:
1) to reflect the pwm initialization:
2) faster value ServoSpeed

Code:


#according to the documentation for the servoSpeed() command
#you are required to initiate a pwm position first.
pwm(D0, 0)

# set the servo speed to 2
ServoSpeed(d0, 2)

:loop

# Set LED On
pwm(d0, 100)

# wait some time for the pwm to do its ramping thing
sleep(3000)

#set LED Off
pwm(d0, 0)

# wait some time for the pwm to do its ramping thing
sleep(3000)

goto(loop)


Digital captures:
User-inserted image


Detailed view:
User-inserted image


Notes:
D0 (ServoSpeed) code
D1 (Manual Fade In/Out) code
#9  
I think (not in front of an EZ-B to test) that you need to use a Servo() command, not a PWM() to set the initial position.

Alan
PRO
Synthiam
#10  
Alan has it - and also, pwm(0) isn't a pwm

Pwm 0 = off
Pwm 100 = on

Neither of those are actual pulsing any width lol:)

So just set something like servo(90) or servo(1) or servo(180) or servo(14) or servo(142)

The servoSpeed() command has a writeup entry in the ezscript manual. You can read it to understand what I'm saying.
PRO
USA
#11  
DJ,

Please a little recognizement for the time i spent testing, troubleshooting (Digital scope), posting etc.

I know difference between PWM 0% and 100%, PWM is not PPM (Servo pulses), and PWM rates etc etc.

I followed your tutorial code (does not work) and than your obvious comments:

Quote:


Oh - and according to the documentation for the servoSpeed() command, you are required to initiate a pwm position first.


you never mentioned 0, 100

I got my lesson: "Wasted time".
#12  
I apologize for not getting this.
Based on the documentation for ServoSpeed, what Alan, and DJ are saying would be:

Quote:


ServoSpeed (servoPort, speed)
Set the speed of servo or PWM. This is the speed to move between positions.
The servo speed is a number between 0 (fastest) and 10 (slowest)
*Note: To initialize the ServoSpeed() at first use, set a Servo() position before using the ServoSpeed() command. If there is no previous position (such as during power-on), the software assumes the position is 0 and will cause issues with your robot.
*Note: Once the ServoSpeed() has been initialized the first time, specify the ServoSpeed() before specifying the Servo() position.
Example: ServoSpeed(D14, 25)


So in my test code i have tried the following:

Code:


Servo(D4, 0)

# set the servo speed to 2
ServoSpeed(D4, 5)

:loop

# Set LED On
PWM(D4, 100)

# wait some time for the pwm to do its ramping thing
sleep(3000)

#set LED Off
PWM(D4, 0)

# wait some time for the pwm to do its ramping thing
sleep(3000)

goto(loop)


so the initial state is set by Servo(D4, 0).
This turns it off. This seems to be required by the ServoSpeed() Documentation.
I then set ServoSpeed(D4, 5) which is halfway between the max and min according to the documentation entry.

this is where I am unclear...
Am I using PWM to set the values for on and off "PWM(D4, 0) and PWM(D4, 100)" then relying on the ServoSpeed() to determine how rapidly it moves through the range OFF and ON?
I suspect the sleep() is there to eat time while ServoSpeed() is moving the voltage up or down depending on where in the loop it is.
I tried using the PWM commands as above, as well as switch PWM() for Servo() and neither performed as I anticipated.

I could just off load to an arduino, but my space is limited in this project and would like to capitolize on the available ports on the IOTiny.

Thank you all for your help =)