Microcontrollers > Microcontrollers/General

Getting started with UNO

(1/4) > >>

OperaHouse:
This thread will cover general issues getting started with UNO.  I wanted someone to start from zero and document every step they did and what problems they encountered.  The only UNO's I have are a real one and several clones made many years ago.  These used the same communication chip.  Now I just use the NANO clones that should not be confused with a real NANO.  Those use the CH340 chip and I had to load the driver off the internet.  There are sites that explain how to do this but they all seem poorly written.  I never documented how I did it, but it took several tries.  At this time there seems to be a deal on ebay.  Search "uno r3 atmega328p" US sellers and one for $2.99 SHIPPED shows up.  This is a full size UNO with the CH340 chip.  It says it will load in the driver automatically.  I have some doubts but at only $3 and getting it in a couple days is worth a try. 

I've been using micros off and on since the late 70.  I don't get too heavily invested in any one as they come and go pretty fast.  Micros are like children.  Every one is different, but they are all the same.  If doing simple logical functions it doesn't matter.  I am still using version 1 of the Arduino and probably should update it.  I'm always concerned they will do something some day to make  the clones incompatible like FTDI did bricking clones.

I demonstrate simple programs in a simple way that I think people will find obvious.  On the internet there are lots of demonstration programs That I think are hard for the novice to understand.  Everyone wants to look like a REAL programmer.  I will write the port number in instead of using a variable name. It will be confusing enough without adding in a bunch of names.

It seems to be the industry standard that every UNO clone come with the sample program BLINK loaded.  Power it up and LED pin 13 will start flashing.  You do not need to use a power supply to make the uno work.  It will use the 5V power from the USB port.   Note the rate of flash because the first thing to do is to change the time delay in the program.   Changing the flash rate of the LED will verify that communication with the UNO was established.  In the tools section you may have to select the board that is being used and the COM port first.   The COM port will usually just pop up on the screen  and it will ask you if that is the poet you want to use.  Sounds complicated already.  I got forced into being the ISO Quality Auditor at a company.  Test procedure had to be written so anyone off the street could come off the street and perform the test, that kind of detail.  On the internet 20 steps are reduced to someone saying just do this.  Your experience will be helpful to others.

If successful with BLINK change, rename the program BASE.  This will become the boilerplate for future programs.  The simple time delay blinking will be changed to a loop counter.  I've found a loop count of 200 convenient to use.  Every line of code takes some time to execute.  The program looping 200 times may take a second or 10 seconds.   Always add in a delay statement of about 20 milliseconds so the blinks will be visible. 

FIRST STEPS

Click on ARDUINO
Then click on FILE
Click on EXAMPLES
Go to BASICS and click on BLINK
The following program will load.

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
 
  This example code is in the public domain.
 */

void setup() {               
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(13, OUTPUT);     
}

void loop() {
  digitalWrite(13, HIGH);   // set the LED on
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // set the LED off
  delay(1000);              // wait for a second
}

digitalWrite is how you write to a port.  13 is the port number as identified on the board.  This pin has a LED connected to it on the board.  HIGH will send 5V to that pin.  HIGH and LOW are fixed variables representing 1 and 0.  They can not be set to any other value.  Any other variable will have to be named first in the program.  Anything after // is a comment that will help you figure out what the program is doing 6 months from now after you have forgotten.  Use lots of them.

delay(1000)  is the time delay routine.  That number is in milliseconds starting with 1.  1000 is one second.  here is an upper limit we will talk about later.

The } is the end of the program and ; is the end of a line.  Forgetting  the ; will likely be your most common error.  The compiler will point out mistakes, but it generally will be in the line before.

So, change the delay times in both statements from 1000 to 3000. Click on FILE and SAVE AS, then enter the name BASE.  Now does it work?  Move the mouse over to the right arrow in the circle and click.  Two LED on the board will start flashing indicating two way communication.  When finished the pin 13 led will flash at a different rate. (if you are lucky)

Two handy things to have are "male female dupont connectors" or male - male depending on micro.  Depending on the length you can get a large number of these for a couple of bucks.  That will allow connection to modules like this and there will be no need to solder.

http://www.ebay.com/itm/5Pcs-MOS-FET-Button-IRF-520-Driver-Module-For-Arduino-Raspberry-Pi-Perfect-/282178805681?hash=item41b32adfb1:g:STEAAOSwFe5X1-kX

These are limited to 24V and 1A which is pretty small.  I don't know why there isn't a 10A module at 200V.  That would be useful and save you a lot of pitfalls of soldering etc.  If anyone has seen one let me know.  My water heater uses a 36V PV string and I just cut out 6oV FETs from old small UPS supplies.  A hand grinder with a thin cutoff wheel can net you a couple 50A FET heatsink and mounting board.
 

Check back as I will update these posts as I get time and will edit the existing post for the two days that I am allowed.

OperaHouse:
I should have started with the compiler can be found and downloaded at http://www.arduino.org/
Click on DOWNLOAD and then follow instructions.  There is also a GETTING STARTED page.
Comments would be appreciated on your experience with the download process.

Assuming everything works so far, copy this code into BASE.  This is the easy start for any new program.  As you learn other routines such as serial and analogWrite, include those sample lines into the BASE program.  Copy and paste is a heck of a time saver, but it allows you to make mistakes even faster.  The verify check mark circle allows you to compile a program and check for errors without having a UNO connected.  After finding and correcting each error, use the down arrow to save the changes.  Don't feel bad about errors.  I made one error (forgetting the ; at the end of the line) in writing this simple program.

/*
  BASE
  Turns on the LED for a quick blink, then off for a longer period, repeatedly.
  A loop counter counts the number of loopa and resets at 200.
  This boilerplate code for future programs is in the public domain.
  The slash and star indicate multiple lines of comments eliminating
  the need of // before each line.
 */
 
// put additional variable names here.
// int means it is an interger -16K to +16K

int blinktime = 0;    // define blinktime and set initial value to zero

void setup() {               
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards.
  // if you forget to declare an output pin, it will still
  // output a pulse for a machine cycle.  Then the pin will
  // float. That can drive you nuts when driving a FET, but
  // can be really handy at times.
 
  pinMode(13, OUTPUT);   // LED pin declared as an output 
}

void loop() {        // the { indicates the start of the code body
 
  if (blinktime == 200) blinktime = 0;       // reset if high limit is reached
  // this is an interesting line. blinktime == 200, The == is a logical statement.
  // blinktime = 0 A single = is a mathamatical.  Confusing these two is easy to do
  // and will not always show up as an error.  When you have written code that looks
  // perfect but doesn't work, look for this.
 
  if (blinktime == 0) digitalWrite(13, HIGH);   // set the LED on.  It will stay
                                                                // on till it is turned off
 
  if (blinktime == 10) digitalWrite(13, LOW);   // set the LED off when 10 loops
                                                // are reached.  As the program
                                                // increases in size this number
                                                // will have to be reduced
                                               
                                               
  // Between these two lines of code you custom program can be
  // written.  Additional blinks could be included to indicate                                             
  // power level or a low battery warning.  A single blink every
  // second or so is there to indicate the micro is powered and running.
 
 
 
  blinktime = blinktime + 1;    // add one to the program loop count
  delay(10);                          // wait for 10 milliseconds. make shorter
                                          // or longer
}                                         // indicates end of program

OperaHouse:
One of the most common uses of a micro is to read voltages.  That is done with
an analogRead statement. The UNO has six analog pins labeled A0-A5. That function
returns a 10 bit number from 0-1023.  Typically the count should be between 600 and
750.  This prevents you from going over the 5V input limit of the A/D converter. You
often have a lot of noise with the reading.  It is best to average a number of readings
to get a good solid number.  The following code takes a running average and also serves
to multiply the raw A/D reading into a number representing an engineering unit. Note
the new variables had to be added at the beginning of the program.

For a visual, at so many loops the battery voltage is compared with a set value.  The
double && is a logical, it means both statements must be true. You get more blinks as
the voltage gets lower. There is still the single short blink to indicate everything
is OK.

A serial print routine has been added at the end of the program.  It sends data at the
199th loop just before the number resets. The line Serial.begin(9600) tells the micro
at what speed to communicate with, 9600 baud.  Without this line it will not communicate
with the Serial Monitor in TOOLS.  Printing takes time and should be limited in how
much is printed and how often.

Load this program into your computer and save it as an example using these basic functions.
Load it into your micro and do the three following tests:
Jumper A0 to REF......It should read 1023 counts and about 18V.
Jumper A0 to GND......It should read    0 counts and about 0V.
Jumper A0 to D13......It should read    0 counts and about ??V.
Ponder that a while.  It will teach you to be careful in what you believe is happening.

In this sample program the voltage is calculated in millivolts.  In a 12V system that gives a number of about 13,000 or down to the millivolt.  There are two problems with this; (1) with this A/D having a maximum count of 1023 the accuracy is only three digits, more than that is imaginary. (2) With int the number can only go up to 32K. Then it will represent itself as negative.  This becomes a problem over 32V.  Printing all those digits will have the numbers dancing around.  For higher voltages , multiply by a smaller number to give tenths of mv.  In printing notice the word (float).  That formats the number to TWO decimal places after dividing by a thousand which is a reasonable number.

Averaging can almost give an extra digit of accuracy, but it has a slow response.  Sometimes it will be better to use the raw data number when response needs to be fast.  I have mixed both in a program.  The count up and down examples shown later have a slow response.  It is sometimes necessary to add a"clawback" when loads change instantly.

The following POT might be useful while experimenting.  Having soldered connections to a pot is more reliable.
 
http://www.ebay.com/itm/New-Rotary-Potentiometer-Model-Sensor-for-Arduino-UNO-PIC-AVR-MCU-DSP-/282248992879?hash=item41b759d86f:g:IZ8AAOSwA3dYIZmb

/*
  BASEAS
  Turns on the LED for a quick blink, then off for a longer period, repeatedly.
  A loop counter counts the number of loops and resets at 200.
  This boilerplate code for future programs is in the public domain.
  The slash and star indicate multiple lines of comments eliminating
  This program blinks about every second to indicate it is running.
  A/D reads value on A0 then multiplies and averages it to get voltage in mv.
  It blinks more as the voltage lowers and serial data is sent to TOOLS.
 */
 
// put additional variable names here.
// int means it is an integer -32K to +32K
int blinktime = 0;    // define blinktime and set initial value to zero
int rawdata   = 0;    // A0 A/D data
int battery   = 0;    // calculated battery voltage


void setup() {               
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards.
  // if you forget to declare an output pin, it will still
  // output a pulse for a machine cycle.  Then the pin will
  // float. That can drive you nuts when driving a FET, but
  // can be really handy at times.
 
  Serial.begin(9600);         // setup serial and define baud rate
  pinMode(13, OUTPUT);    // LED pin declared as an output 
}

void loop() {  // the { indicates the start of the code body
 
  if (blinktime == 200) blinktime = 0;          // reset if high limit is reached
  // this is an interesting line. blinktime == 200, The == is a logical statement.
  // blinktime = 0 A single = is a mathematical.  Confusing these two is easy to do
  // and will not always show up as an error.  When you have written code that looks
  // perfect but doesn't work, look for this.
 
  if (blinktime == 0) digitalWrite(13, HIGH);   // set the LED on.  It will stay
                                                // on till it is turned off
 
  if (blinktime == 4) digitalWrite(13, LOW);    // set the LED off when 4 loops
                                                // are reached.  As the program
                                                // increases in size this number
                                                // will have to be reduced
                                                                                             
 
  // GET ANALOG DATA AND CONVERT TO BATTERY VOLTAGE IN MILLIVOLTS
  rawdata  = analogRead (0);                    // Anolog In.  Read the battery voltage  PIN #A0  BATTERY
 
  battery  = battery - battery / 18;            // Subtract one average reading   
  battery  = battery + rawdata;                 // Add latest A/D reading back
                                                // 3.3V = about 13500 counts or 13.5V
   
  // Display battery condition by number of blinks
  // First long blink if battery fully charged
  if (blinktime == 60 && battery <= 13750) digitalWrite(13, HIGH);    // set the LED on if below set voltage
  if (blinktime == 70) digitalWrite(13, LOW);           // set the LED off regardless at this time
 
  // Second long blink if battery medium charge
  if (blinktime == 90 && battery <= 13200) digitalWrite(13, HIGH);    // LED on if below set voltage
  if (blinktime == 100) digitalWrite(13, LOW);                 // LED off regardless at this time
 
  // Third long blink if battery low
  if (blinktime == 120 && battery <= 12500) digitalWrite(13, HIGH);    // LED on if below set voltage
  if (blinktime == 130) digitalWrite(13, LOW);                   // LED off regardless at this time
 
  // Fourth long blink if battery discharged
  if (blinktime == 160 && battery <= 12000) digitalWrite(13, HIGH);    // LED on if below set voltage
  if (blinktime == 170) digitalWrite(13, LOW);           // LED off regardless at this time
 
       
 
  // THE FOLLOWING IS THE SCREEN PRINT ROUTINE
  if (blinktime == 199)                         // if on the last count print data
   {                                                   // do everything after the { symbol
   Serial.print(rawdata);                       // A/D value
   Serial.print(" count   ");                    // space, then count, then some more spaces
   Serial.print((float)battery/1000);        // battery voltage TO TWO DECIMAL PLACES USING FLOAT
   Serial.println("V  ");                          // V for volts,  Notice the ln after print.  NEW LINE
   }                                                   // } symbol ends the things to do for this IF
   
  blinktime = blinktime + 1;     // add one count to the program loop count
  delay(10);                           // wait for 10 milliseconds. make shorter or longer to suit
                                 
}                                         // end of program loop

george65:

On your recommendation above, I have bought a couple of the boards. Thought I'd get extra to start with so if I fry one or something doesn't work, I can verify it's the idiot not the machine that is at fault. Cost me all of $4 Au. They must make these things for .50C each. amazing for the power they have.


I'll now look for a kit with all the wires and some shields and bits and pieces to get started learning with.  I'd like to make a solar tracker. Not for a panel but to keep a reflection focused on my neighbours window who built the place illegally and is always looking into my backyard.
Other immediate thing that comes to mind would be a controller for an Oil burner.  This could control fuel flow and maybe air either with a throttle body from a car or chopping the power to the blower motor. I don't know what they are capable of  or more so what I can figure out but i have found that practical projects always teach one a lot more than doing something just for the hell of it.

Seems what you are putting up here is going to be good, practical and useful learning.
Thank you for your efforts and contributions to help others.

OperaHouse:
It is good to have several because it is likely one will be destroyed and one can serve as a test bed to work out the bugs of a software change before the working system is altered.  I would avoid kits  and just buy what you need. If you hadn't noticed the ones with the smaller soldered in processor are cheaper. A replacement micro for the socketed boards is as much as an entire board.

Navigation

[0] Message Index

[#] Next page

Go to full version