Arduino 101 - Lesson 2

Discussion in 'Welcome to the DARK side' started by paul_l, Jul 27, 2017.

  1. paul_l

    paul_l Staff Member Administrator

    Messages:
    9,860
    Likes Received:
    5,917
    Joined:
    Dec 5, 2015
    First lets recap what we have done so far

    Downloaded the software and installed it.

    Connected the Arduino and determined the COM port - Note when Tom tried this on his laptop the device manager didn't display any Ports until he connected the Arduino.

    Configured the Arduino software for your Arduino - Port and Model

    Loaded a Sketch (the arduino term for a program) into the software and uploaded it to your arduino.

    Lesson 2 - Playing with LED's

    Connect the arduino

    We will start with the Blink Sketch

    So if you have loaded the Blink sketch, open the Arduino software

    Go to File > Examples > 01 Basics > Blink

    If you have already opened the blink sketch last it will open that.

    [​IMG]

    Click on the Verify button data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAlCAIAAACs1XwQAAAJPUlEQVRIiY2W63Pc1BnGX2nXhpZOabHXl5gQSGkmOE5CS0iYDLcyAZrsaqVd3whDEq+kI+lIu0tNy3SgIcUJJCFxnEtD4iR4d7XadRxCmNi7ks6RtDYMnfIV/gP6n/TDMb6EdNqZZzRHGs35nec5r14d+Nd3PzBdrBWeHtmZGDv4ACpATm/HRVA1Dum8guMKbpPNuGzGZZNXTF7B91X8J+IVDCuAf1TM5974XeehITiCYpoFY3KbVQBV+ymAU/F9dV/qKuBaJb87O9BzZDSmmrxmcDqGw2O8YXKyEZONWA7HcpiTMSgYVGNVyo/X+4mTjTWAkvH8yM7E4WFOxYB0UFRAGqdjyGntehFyJshWuzEOyOStYs/ER785O/XUxctbpy71Tnz0s7f/DJoJCAPCywPNBNXgkbkKKNn5PZn+zkNZXjNAVsAwYlYecgqPTJCNGCqCmu89dvKl2duZoJVs+qIfpD2a9miGRhIJX567venUGQ7nARkgI063QDVA1tYASsZzg/2PHM5whg4aAlUBDcV0DLIOit7x3rHXv2xIfiS4JO0HaUJEn4g+SXle0nXThIiUJhvNfV/c2XD8BGg6KCiGrRi2VgE3bqC9wwOdY4OgIdANQCrk1LiG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lJ2estNbqd0zXtEq4ORcPnteeera2a6q02XXuqv1bqeWsKuJer3DcRJ2NWFXO+xqV9Xpq9Q3VuY2lGs9Zae7VO0pOxvsOtM9s68DnLpdzEzJW29MbqjPdZZrneVab+1mV9XpsKudVafXcTbUat1OtbNi95acvkq9t1Lrtms91Xq3XeuqOEzddu0erQLOzOZHp9Tt1yb7qrNd5ZuJys0e5/NExUnY1W7b7imVN1Sr3U6ty65tsG/2lOvddq3r/9Aq4Fyt+OaktmN6sq8622XPJSo3e+ybvTP25unrO2dKO65cHbj86c7pG9uvXNvx6fVnrpV2Xr2+Y3p6x/+6/gfVqm/09SkE0QAAAABJRU5ErkJggg==

    The arduino software should verify the sketch (and hopefully have no errors)

    Then click the upload button data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB8AAAAlCAIAAAA1LaWlAAAHVUlEQVRIiY1Wa28bxxW9u5TQuE6RoFZkSY4fUWMXLQoU9acCLdo6TuMHuS8K7bc+pN3Zmdml/JZdJ/ErjhpbtRSlKYIajURSomnZlh8Sd3ceu0sZ6M8p+hvSD0PRsqyqBQ4uwOHsmTtn7j0zMFevbUT1ZdRqL2IXc6/ELVGr1eBf33yzCf/eFptmbj8f+n4/uiX6f/cC28zZNHMTQHfpluhZx/b/bo//yr4JmvcS/s+vQBsj4BIVt4O3Ia5j47dbx4Ifgkd6yTi4GBDVcAgu1nAIHgFEAdEdp88PXrs5PDX9g8+/PDTzxZ4bkzvPTgCi4BEdBYAojGENh5pHAdHNUc2DMV8nFUAESAiIgIcL4an9n00debB8YiWymCzL3OapIzKLSTMR7y0tD9+6U6Dj4AfgYvAD8INuQi9QwBRcpGEKLgKEwfOB0L6Prxx9tHxiZbWUJJYQBmMGY8U4NhhTIzYXxVb0/vLjNy79SQ9CwKTz+QZoCIOOgh4SgEs0TMHFveH43slbxVZkxPw3edtk0pbSZNISwpG5LaWRCJNzi0mbCxUPTs+Cj2EUFWgIiALCgKiGsOZR0N0AXAJjSsTgwJ+n7CQdEXl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fFOj4+w+flPm6MfG8uMoc2T7ZYqr2TzyLR+SaEspkqeomR7YdkRVbybHHK0rxjVi/mzyi4fDNix+WeVZqcbWAahmTpaVY2CJXPaXcQtmA0r3YSkbS9uC1m6o/X2LvJePgkoIfwh8R+MH+ydsjol3muZWkxVVWjPhI9lzpq45R1ZJyGFXBZiIOTs9u0qSrDNY8qqOgE3E4fGvaaHFl3yq7kez5idVE9X3X2rr9cXB6FjysNNjMro0R3aUwigt+CKO+uqHevPjRr+4vK2/oHB1LTZkbIjseMUN07OiDp61dH19fv+jJVrqP4h4U9qAQRrGOAh0F4IfghzoZ3zc59Yt7D8uiXWpxZevFiKtWeO/Bk3duTYMfAh0HDwMONFoBj3SeVggrwKbVXloZUfCDb5+5MHD1k3enZn44+7dDd2YHr938zvlL6oYDFAAKOs8NFGieeuBhDSHwke6h7dg1j8IY7jy7PKqjoOCHOgrAJd3nHyAKPgWfKvbesY3sGHQPb4MCIj0+7fFpAZHuoIawhpCGXA0h3UOd8fXXq+6qRwfWXeWR28CnWwzSAAgFioFQwLSjjF8BvwK4ArgCpAKkAn4FDszVt8E78wuvjuyrNgZri4O1xlC1sXe+sW+uceDrxoGvG3vnG3uqi0O1xT3Vxb3V+r75OgzWGttgoLq4e35hoLo4VL83VL+nRt6qLvQtNHctLPXVl/rrS4PVDnbXmn0LzV0Lzf56s7/e6K83/gf724v3h+r3BqqL6udQ/d6ehebbi/e7uQ9Vm0PV5lC1MVRtDNYa/fVGf725u9ZUK/0H+jm2yz/OGVYAAAAASUVORK5CYII=

    This will upload the blink sketch, the TX & RX LEDs will flash - these are the Transmit TX, and Receive RX LEDs which indicate the sketch is being transferred. If everything went OK the L LED should flash on for 1 second then off for one second.

    OK so what happened

    The first part of the sketch is the description

    /*
    Blink
    Turns on an LED on for one second, then off for one second, repeatedly.

    Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
    it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
    the correct LED pin independent of which board is used.
    If you want to know what pin the on-board LED is connected to on your Arduino model, check
    the Technical Specs of your board at https://www.arduino.cc/en/Main/Products

    This example code is in the public domain.

    modified 8 May 2014
    by Scott Fitzgerald

    modified 2 Sep 2016
    by Arturo Guadalupi

    modified 8 Sep 2016
    by Colby Newman
    */

    the /* denotes the start of a comment section and the */ denotes the end. Anything between them has no effect on the sketch, but is there to help in the future, and describe what the sketch is for.

    In more complicated sketches, you would normally have variable defined, but this is a very simple sketch.

    All sketches have two main sections

    Setup

    // the setup function runs once when you press reset or power the board
    void setup() {

    Loop

    // the loop function runs over and over again forever
    void loop() {

    The { bracket marks the start of the section, and the } marks the end of a section.

    Setup

    // the setup function runs once when you press reset or power the board
    void setup() {
    // initialize digital pin LED_BUILTIN as an output.
    pinMode(LED_BUILTIN, OUTPUT);
    }

    Loop

    // the loop function runs over and over again forever
    void loop() {
    digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
    delay(1000); // wait for a second
    digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
    delay(1000); // wait for a second
    }

    Now time to play

    Change the values in the delay from 1000 to 250

    Click on verify, then click on upload.

    What happens .......

    Have a play and try different values in the delay

    Paul
     
  2. Toto

    Toto I'm best ignored Staff Member Founder Administrator

    Messages:
    15,419
    Likes Received:
    3,842
    Joined:
    Dec 5, 2017
    It is not to bad actually. I even got it to work ......... must be easy. Once you start to play about with the two sets of values as Paul has done above to change the the frequency and time of the flashing, you will quite quickly understand the relationship between the open and closed switching.
    It is good fun and you feel like you have achieved something. However, ,........ a bit at a time for me ..... nice and easy at the helm Captain. :avatar: ...... Looking forward to the next lesson.:thumbs:

    Toto
     
  3. Ron

    Ron Full Member

    Messages:
    3,337
    Likes Received:
    784
    Joined:
    Dec 5, 2015
    Not got my bits yet so I'll have to catch up when I can!!
     
  4. Toto

    Toto I'm best ignored Staff Member Founder Administrator

    Messages:
    15,419
    Likes Received:
    3,842
    Joined:
    Dec 5, 2017
    We won't be jumping too far in front Ron. I was having the crack with Paul earlier tonight and there are a good number of projects that can be done just by having the basics. Loads of good ideas for the use of lights for the likes of level crossings, traffic lights, timed streetlights and all sorts.

    Something for everyone I think.

    Cheers

    Toto
     
  5. paul_l

    paul_l Staff Member Administrator

    Messages:
    9,860
    Likes Received:
    5,917
    Joined:
    Dec 5, 2015
    Not a problem Ron, I'm in for a hectic couple of weeks at work, so there will be couple of more LED examples, then wait for everyone to catch up.

    Paul
     
  6. paul_l

    paul_l Staff Member Administrator

    Messages:
    9,860
    Likes Received:
    5,917
    Joined:
    Dec 5, 2015
    Part 2

    Now lets move the from using the onboard LED to an external one.

    [​IMG]

    Using the bread board

    Connect a wire from the GND connection of the Arduino to the -ve rail of the bread board.
    Connect a wire from pin D11 to the first column of the breadboard
    Insert an Led into column 1 & 2 - the long lead (anode) into column 1 and the cathode into column 2
    Connect a 1K resistor to the Ground (-ve rail).

    Make the following changes to the Blink sketch replace LED_Builtin with 11

    /*
    LED Blink

    Lesson 2 use an external LED

    */

    // the setup function runs once when you press reset or power the board
    void setup() {
    // initialize digital pin LED_BUILTIN as an output.
    pinMode(11, OUTPUT);
    }

    // the loop function runs over and over again forever
    void loop() {
    digitalWrite(11, HIGH); // turn the LED on (HIGH is the voltage level)
    delay(1000); // wait for a second
    digitalWrite(11, LOW); // turn the LED off by making the voltage LOW
    delay(1000); // wait for a second
    }

    Verify and upload the sketch

    Paul
     
  7. Toto

    Toto I'm best ignored Staff Member Founder Administrator

    Messages:
    15,419
    Likes Received:
    3,842
    Joined:
    Dec 5, 2017
    I think I'm understanding that. It will be easier when I have it constructed with the bits in front of me and the arduino switched on. Going on part 1, it was quite simple, familiarisation will help as well.

    Cheers

    Toto
     
  8. paul_l

    paul_l Staff Member Administrator

    Messages:
    9,860
    Likes Received:
    5,917
    Joined:
    Dec 5, 2015
    well as you're grasping it - here's a set of crossing lights

    [​IMG]

    Add a second LED connected to pin D12 and resitor etc.

    then change the sketch to add the extra lines for Pin 12 shown in bold

    =====================================================

    /*
    LED Blink
    Lesson 2 b two flashing LEDs

    */

    // the setup function runs once when you press reset or power the board
    void setup() {
    // initialize digital pin LED_BUILTIN as an output.
    pinMode(11, OUTPUT);
    pinMode(12, OUTPUT);
    }

    // the loop function runs over and over again forever
    void loop() {
    digitalWrite(11, HIGH); // turn the LED on (HIGH is the voltage level)
    digitalWrite(12, LOW);
    delay(1000); // wait for a second
    digitalWrite(11, LOW); // turn the LED off by making the voltage LOW
    digitalWrite(12, HIGH);
    delay(1000); // wait for a second
    }

    ============================================================

    Play with the delay settings and see what you think.

    Paul
     
  9. Toto

    Toto I'm best ignored Staff Member Founder Administrator

    Messages:
    15,419
    Likes Received:
    3,842
    Joined:
    Dec 5, 2017
    Looks doable.:tophat:
     
  10. leadie69

    leadie69 Full Member

    Messages:
    316
    Likes Received:
    183
    Joined:
    Dec 31, 2015
    I'm liking this series so far - well done Paul.
     
  11. ianvolvo46

    ianvolvo46 Staff Member Moderator

    Messages:
    5,354
    Likes Received:
    1,616
    Joined:
    Dec 8, 2015
    This looks fun/magic/spellbinding tempted to go for it as I reckon you've got to have the 'hard dina you know' in front of you to remotely (dcc speak) understand it.

    Ian vt ... feeling drowsy :facepalm:
     
  12. Toto

    Toto I'm best ignored Staff Member Founder Administrator

    Messages:
    15,419
    Likes Received:
    3,842
    Joined:
    Dec 5, 2017
    So is that another to the ranks ?
     
  13. ianvolvo46

    ianvolvo46 Staff Member Moderator

    Messages:
    5,354
    Likes Received:
    1,616
    Joined:
    Dec 8, 2015
    noooo just curious ... honest ... that milk of newt tasted off though

    Attached files [​IMG]
     
  14. ianvolvo46

    ianvolvo46 Staff Member Moderator

    Messages:
    5,354
    Likes Received:
    1,616
    Joined:
    Dec 8, 2015
    The 'hard dinna you know' is on its way.. only cos I'm too old for Lego and it looked nice..

    Ian vt:facepalm::facepalm:
     
  15. Toto

    Toto I'm best ignored Staff Member Founder Administrator

    Messages:
    15,419
    Likes Received:
    3,842
    Joined:
    Dec 5, 2017
    :avatar:

    Captured.
     
  16. ianvolvo46

    ianvolvo46 Staff Member Moderator

    Messages:
    5,354
    Likes Received:
    1,616
    Joined:
    Dec 8, 2015
    Well I thought we can't have Paul's lessons falling on just one pair of deaf ears ... heroic of me really.

    Ian vt:facepalm:
     
  17. SMR CHRIS

    SMR CHRIS Staff Member Moderator

    Messages:
    3,323
    Likes Received:
    545
    Joined:
    Dec 31, 2015
    Don't worry I'm watching this info as well, for the time I decide that New O scale Turn Table needs motoring with a steeper motor and alignment won't be an issue as once it's all set up it should just be a mater of selecting the track number and your wish is it's comand,:avatar: well that's the theory :scratchchin:
     
  18. Ron

    Ron Full Member

    Messages:
    3,337
    Likes Received:
    784
    Joined:
    Dec 5, 2015
    That's 4 of us then??:scratchchin:
     
  19. Toto

    Toto I'm best ignored Staff Member Founder Administrator

    Messages:
    15,419
    Likes Received:
    3,842
    Joined:
    Dec 5, 2017
    Born suckers. :facepalm:
     
  20. SMR CHRIS

    SMR CHRIS Staff Member Moderator

    Messages:
    3,323
    Likes Received:
    545
    Joined:
    Dec 31, 2015
    Toto wrote:
    Pot calling the kettle Black me's thinks :avatar::giggle::avatar::giggle::avatar::giggle:
     

Share This Page