GIS-PythonElectronicsWithArduino

From ArduinoInfo
Jump to navigation Jump to search


HOW-TO for the GISElectronics YourDuino Workshop Kit and PYTHON

Trying Python and Arduino:

[INTRODUCTION]
[LESSON1: GETTING STARTED: Check the RoboRED Microcomputer]


The GIS-PythonElectronicsWithArduinoYourDuino MAKERSPACE WORKSHOP KIT

We will use the kit and the components in it to learn how Arduino works and to explore many of the devices you can use with it.

You will use both the Arduino IDE programming environment and PYTHON with the Python IDLE editor to write your own code. You will learn how to SENSE things in the Real World and control ACTUATORS to control your own devices.

750px-MakerWorkshopMicrocomputerNotArduino2-900.jpg

INTRODUCTION

These pages will show you how to use each of the components in the kit, and give you code examples in ARDUINO (C++ like), PYTHON and MIXLY (Drag-Drop) Sketch examples for each one. Then you can combine some components to make examples of Automatic Systems such as a lighting controller or a automatic NightLight for your room. Or an automatic CatFood Door ... OR ???

You're probably here because you're interested in MAKING things with Arduino. The first questions:


To get started, we think you need 4 things:

  1. Clear Information
  2. Practical, Low-Cost hardware to learn with
  3. Easy-to-use software to make your own code
  4. Detailed How-To DO IT.

NOW: Making things with Electronics and MicroComputers is different than Woodworking or Metalworking.

There are a couple of little problems: Uh Oh!

ElectInv.gif

  • AND, Software is INVISIBLE !

InvisibleSW250.jpg

NO WORRIES! We will show you the ways to make these powerful tools Visible so you can use them.


START HERE

GETTING STARTED:

Check out the YourDuino RoboRED

RoboRED-Annotated-1024-0220-2.jpg

The YourDuino RoboRED is now used in your kit. (Yours may have a Black Board but it's the same. ) It is equivalent to an Arduino UNO but has several added features, like easy-to-connect pins on every Input/Output pin. Near the upper left is the PWR LED which should light up whenever the board is plugged into a USB connection, or has power from an external power supply.
At the upper left there are two added red LEDs which will blink when a software sketch is being downloaded to the board, or other data is being transferred over USB.
In the center is a red LED which is internally connected to pin 13 through a current-limiting resistor. It should blink after you load the example BLINK software sketch. The colored 3-pin connectors on every I/O Pin make it easy to connect many input and output devices. Vcc(+5V) pins and (Ground) pins with every I/O pin are good for connections to breadboards etc.


HOW DOES THIS WORK??

ElectInv.gif

Look at an Ipod or a RoboRED or a CellPhone or some other circuit board. You can't see the electrons moving. All you can see or hear is the Results of electricity.
If you want to make things and get results, you need to know what the electrons are doing. And Why. And How.

And, by the way, SoftWare Is Invisible Too!
You can't see the the code that some people wrote that makes your CellPhone or TV or Ipod work.

Not To Worry! Your kit and the ArduinoInfo.Info website is about making Electronics and Software visible to you, and understandable to you. You can learn how to make Electricity visible, how to trace it, how to measure it, and how to tell what it is doing. And you can learn how to create Software "Sketches" that create new and interesting high-tech things.

If we understand this stuff, we can start to make many more-interesting things work. Let's THINK about it. We have wires and circuits and electricity. It's easier to think about it if we use some SYMBOLS to make DIAGRAMS. Let's look at This Diagram :

Rails3.jpg

Slow Down Time: Look carefully:

Your RoboRED (Arduino) runs on Electricity. A supply of '5 volts' comes from the USB cable or batteries. At the top and bottom of the diagram there are two main connections:

5 VOLTS

GROUND

Find them on the diagram. What about "RAILS"? A lot of things connect to that 5 Volts and that Ground. Tekkie-Speak calls them 'rails' like railroad rails running through a project or circuit. Let's look at that diagram in detail. NOW, Remember when you tried out the BLINK sketch??

You told Arduino to connect Pin 13 (Find it??) to HIGH (The 5V Rail). It did it. And the LED lit up. What happened when you told it to connect Pin 13 to LOW (Ground?)

Soon you'll hook up a switch like on the diagram. Then lots of other interesting things.



How to connect wires to your Arduino

RoboRED-Annotated-1024-0220-3.jpg

Take a close look at the RoboRED (An Arduino UNO compatible):

See the long sections of RED pin strips labelled "+ 5 V" Those are the +5V Rails.

See the long sections of BLUE pin strips labelled "Ground" Those are the Ground Rails.

You will hook lots of things to those rails.

See the long sections of YELLOW pin strips labelled with Pin Numbers. Each of those connects to a different pin. There are Digital Signals like the pin 13 you used, and Analog Signals you will use soon.

Look Closely Now: See the "Pin 13 LED" and "Pin 13 RESISTOR" marked on the RoboRED. Those are the parts shown on the Diagram, and the actual parts you controlled with your BLINK Sketch.

STRUCTURE OF ARDUINO SOFTWARE:

Every Arduino Software Sketch has two main parts:

  • SETUP - Runs Once at the beginning
  • LOOP - Runs over and over again, forever

What SETUP does: Tells Arduino about things that need to be done once. Arduino Digital Pins can be either INPUT or OUTPUT. MIXLY will take care of setting that for us!

What does VERIFY do??
A LOT! More details later, but Verify is a program in your main computer that goes through every Instruction in your Sketch (Ignoring "Comments") and checks it against the list of valid Instructions, and checks that the structure and sequence of the statements in the Sketch are correct. Fussy, Fussy!

You will see a LOT of things roll by in the bottom MIXLY window. If they're OK, then it "compiles" or "translates" the sketch into the machine code that Arduino actually runs on. It saves that 'ready-to-run' code for you to Upload to Arduino and run. Other systems would call this process "Make" or "Compile".

What does UPLOAD do??
First, Upload runs Verify to check and compile your program. Then it communicates to your Arduino over the USB connection, resets the Arduino chip, and talks to software already on Arduino (called the BOOTLOADER(W)) to load your new program into the Arduino's memory. Then it restarts Arduino and your program runs it's SETUP section and then repeats the LOOP section.
[NOTE: The (W) Means this is a Wikipedia link.]

Instruction: digitalWrite


This instruction sets an OUTPUT PIN to either HIGH (connects it to +5 V) or LOW (Connects it to GND).

Remember: HIGH = 1 = ON = 5 Volts and LOW = 0 = OFF = 0.0 Volts

So, the first line in LOOP sets PIN 13 to HIGH. This means Pin 13 is connected to +5 Volts, and current flows through the resistor and LED that are already connected to pin 13. The LED lights up.

Instruction: delay


The delay instruction just waits for a period of time. The VALUE used with delay is in Milliseconds (1/1000 second). So delay(1000); waits for 1000/1000 seconds (1 second). We'll change that soon.

Notice that each time you do this the LEDS that are marked "Tx" (Transmit) and "Rx" (receive) flash as your main computer communicates with your Arduino.

Try some combinations of ON and OFF delay() times. Like ON 1000 and OFF 50.

Try making both ON and OFF times shorter and shorter. If you make the ON and OFF times short enough your eye will no longer see blinking, because of "Persistence of Vision"(W) which happens when there are more than about 25 blinks per second. So, hmmm.... if you make the LED be ON for 1/50 of a second and OFF for 1/50 of a second that should do it. So try 1000/50= 20 Milliseconds. Put 20 for both the ON and OFF times. What do you see?? How about 10 Milliseconds each? Depending on your personal eye's physiology, at some speed you will not see any more blinks. Younger people can usually see faster blinks.

All right. You're the Programmer! And as we promised next we'll figure out how to use PYTHON to control your RoboRED. We'll start hooking up the Electronics Parts in your Kit! And we'll give you example Software Sketches for most of them. And you'll start writing your own Real World PYTHON


How to use PYTHON with ARDUINO

In the previous section, you uploaded the Blink sketch to your Arduino board. Arduino sketches are written in a language similar to C++ and are compiled and recorded on the flash memory of the microcontroller when you press Upload.

Unfortunately, it is not possible to directly run Python code on most Arduino boards like the Arduino UNO or our RoboRED. Here are the key points to understand:

  • Arduino boards like UNO use microcontrollers with limited memory and processing power that are not capable of running a full Python interpreter.
  • Arduino boards are programmed using C++ in the Arduino IDE, not Python.

However, you can write Python code on your computer to communicate with and control an Arduino board over the serial connection. This allows you to write the main program logic in Python while using the Arduino for hardware interfacing to Sensors and Actuators.

We will use a Python library called Telemetrix to send commands from Python on a computer to an Arduino.

Fortunately, there are standard protocols to do all this! We will use Telemetrix4Arduino. This protocol establishes a serial communication format with Arduino that allows you to read digital and analog inputs, as well as send information to digital and analog outputs

But first you need to use the Arduino IDE to compile and upload Telemetrix4Arduino.

The details are HERE.

PYTHON! Back To The Future