Robots and electronics – Arduino (Advanced)

Now, we learn about an open source electronic prototyping platform that allows us to create interactive electronic objects.

There are several Arduino versions available that can be purchased, preassembled, or do-it-yourself kits. Arduino can interact with the environment by receiving input from a variety of digital and analog sensors, and can produce and trigger actions to affect its surroundings by controlling lights, motors, and other actuators using its analog and digital outputs.

Getting ready

You need the basic knowledge in electronics. For more information, visit http://www.arduino.cc.

Then, you will need the following:

How to do it...

Let's now see the steps involved in preparing an Arduino board.

  • Preparing the Arduino board:
    1. Load included sketch.pde (downloaded ZIP file) into Arduino ROM using Arduino IDE application.
    2. Create your hardware (circuit) on the breadboard and Arduino board.
    3. Connect Arduino to your PC via a USB cable.
    How to do it...

    Arduino installation's live image following the previous schema:

    How to do it...
    1. Copy comMG.au3, arduino.au3, and commg.dll, and paste them into your script folder.
    2. Open SciTE from the AutoIt Program Group, and start writing one of the example scripts. Note that we have to change number 3 parameter at conecta(3,9600) to match your Arduino port in case you get a different port on connection.
    3. Now, run the following scripts by pressing F5:
      • The following code is for digital input:
        ;Example 1 digital input (Press button): 
        #include <arduino.au3>
        conecta(3,9600) 
        While 1 
        $sbutton = digitalread(2) 
        If $sbutton = 1 Then MsgBox(0,"","button pressed") 
        Sleep(50) 
        WEnd 
      • The following code is for analog input:
        ;Example 2 analogic input (Light Sensor): 
        #include <arduino.au3> 
        conecta(3,9600) 
        $initial = analogread(7) 
        $initial -= 50 
        While 1 
        $ldr = analogread(7) 
        If $ldr < $initial Then MsgBox(0,"","Less light") 
        Sleep(50) 
        WEnd 
      • The following code is for digital output:
        ;Example 3 digital output (Switch lights on/of): 
        #include <arduino.au3> 
        MsgBox(0,"Light Output Autoit", "Enter to blink") 
        conecta(3,9600) 
        While 1 
        digitalwrite(11,1)  ; On
        Sleep(1000) 
        digitalwrite(11,0)  ; Off
        Sleep(1000) 
        WEnd 
      • The following code is for analog output:
        ;Example 4 analogic output (Color Lights): 
        #include <arduino.au3> 
        conecta(3,9600) 
        While 1 
        $r = InputBox("Red Light","Value between 0-255") 
        $g = InputBox("Green Light","Value between 0-255") 
        $b = InputBox("Blue Light","Value between 0-255") 
        analogwrite(11,$r) 
        analogwrite(12,$g) 
        analogwrite(13,$b) 
        WEnd 

How it works...

The arduino.au3 library includes some basic functions to enable Arduino analog read/write, digital read/write in a simple way. AutoIt codes have not been compiled with cross-compiler, but it works interactively using an Arduino to Autolt port for electronic management. You write the program in your Windows computer, and you run it from Windows. Then, the Autolt port sends/receives messages from Arduino. So, you must be connected to a Windows PC for establishing a connection. See photos.

arduino.au3 functions read button press, temperature, read digital sensors, or maybe even read a GPS signal. You can create your own Arduino sketches.

You can use it for outputs, to move servos, motors, lights, and switches.

Arduino has several connectors as analog/digital outputs/inputs:

  • Digital: Shows on/off) status, 0V for on and 5V for off. Button digital signals have open/close.
  • Analog: Includes any intermediate voltage.

For example, consider an LDR (Light Depending Resistor). We generated a resistance that decreases voltage as a function of the surrounding light, it measures light voltage variation. Arduino will get/return a value between 0 and 1024. 0 is 0V and 1024 is 5V.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset