#1

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)

#2

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 EZ Builder'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 EZ Builder.

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)

#3

"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.

#4

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. EZ-Builder 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

#5

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 ?

#6

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.

#7

I will try this as soon as I get home.

#8

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.

#9

@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

#10

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