All Tutorials / Steve G / Using ADC (Analog to Digital) Ports, Commands and Controls.

Using ADC (Analog to Digital) Ports, Commands and Controls.

Step 5. Step 4. Controlling servos with sensors and ADC ports.

One possible use for ADC ports, is to utilize them to control servos connected to digital ports respectively, using pressure or light sensors. In the following example, a servo and Photosensitive sensor will be used. The servo will move depending on how much light the sensor is detecting. This will also show an "ADC Value" control in action. It is not necessary to use this control for this example, it is there for demonstration purposes.

So, here is an example of how to do it...

1.) Open your copy of EZ-Builder, and click "Project".

User-inserted image

2.) Click on "Add Controls", then "Scripting" and select "EZ-Script".

User-inserted image

User-inserted image

3.) Now click on the "Gear" icon to open the script editor.

User-inserted image

4.) Copy/paste or wright in the following script...


Servo(d0, getadc(adc0))

6.) Now click "Save".

7.) Next, Connect the sensor to an analog port on your EZ-B, then a servo to a digital port.

User-inserted image

8.) Power on your EZ-B, and connect it to EZ-Builder. Now press "Start" on the script control, and the servo will now move when the sensor detects different brightness of light which change the analog readings. As an optional extra, you can add an ADC control like the "ADC Value" control seen in the video below, to see the sensor readings change as they happen. Check out the short video demo of this in action...

One thing to note, because most servo positions only go up to 180, and ADC values go up to 255, you may need to add a multiplier to the GetADC() script command for each of the positions for better accuracy. The resister values on the Photosensitive sensor determine the accuracy and voltage being returned. An explanation of a multiplier can be found in the step 7.