This wiki has been archived and made read-only.
For up-to-date information about TkkrLab and it's projects please visit our main website at tkkrlab.nl.

HowTo simple Arduino project

From

Revision as of 08:41, 13 June 2018 by FFAA5E (Talk | contribs) (Programming language)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Simple Arduino sensor & actuator projects

The Arduino is an open hardware mircocontroller platform. A microcontroller can be used has the hart of a device that responds to events with certain actions. Sensors are used to sense that certain events or conditions have occured. Actuators (including displays) are used to perform the actions.

What?

A project starts with the formulation of a short description of what one want to achieve. Questions that need to be answered are

  • What do I want to sense?
  • What do I want to act or display?
  • With state or not? (Time words: after, when, ..)

An example of a stateless device is a light switch to switch a light on/off. An example of a device with a state is a lamp that is triggered by movement: the light goes on when some movement is detected and stays of until for 10 second no movement has been detected.

It is a good idea to draw a State Diagram, starting from the initial state and thinking about all the events/triggers that could happen. For each of this a line is drawn to a state. And then continue on with a state that has not been investigated yet. Whenever a state is reached that is equivalent to an earlier state, one can draw the line to that state.

Sensors

There are three types of sensors that can be used with the Arduino to perform some kind of measurement or register an event in the environment. These are: on-off, analog, and serial.

On-off

Examples of on-off sensors are:

Analog

The Arduino can measure a voltage between 0V and 5V with 1024 levels.

Examples:

Simple, but low resolution and/or accuracy.

Serial (digital)

There are sensors which return a numeric value by means of a serial protocol. A serial protocol consists of a sequence of pulses, where the lenght of the position of the pulses code a on-off (digital) value. The sequence of the on-off values represents a numeric value. Usually, the sensor is first send a signal to perform a measurement and then will respond with an answer. A number of commonly used protocols are:

Examples of such sensors are:

Choicing the right type of sensor

Often it is the case that more than one type of sensor can be used to measure something, like for example the temperature. The choice depends on the kind of measurement one wants to do. Think about the resolution, the accuracy, and the frequence of the measurement.

Actuators

There are also three types of actuators that can be used in combination with the Arduino to cause some action or display some information: on-off, analog, and serial.

On-off

The Arduino can be used to switch something on or off. Examples are:

  • LED
  • Relay shield

Analog

The Arduino can also give an 'analog' signal. It is not really analog, but uses Pulse-width modulation (PWM) at a frequency of 500 pulses per second. The width of the pulses can be adjusted in 255 levels.

Examples of uses are:

Serial (digital)

The Arduino can also send digital information through a serial protocol. These are often used to display information on a display.

Displays

Types of displays are:

Actuators

Some examples are:

Programing

For a simple sensor Arduino project one will often use a loop that is executed over and over again after the Arduino has been initialized. In the loop the following steps will be performed:

  • Read sensors
  • Make decision (on state)
  • Activate actuators
  • Sleep

Program structure

The Arduino IDE makes use of Sketches, where each sketch represents one program to be executed at the Arduino. From this it is also possible to include libraries for certain sensors or actuators

  • Include statements of libraries
  • Pin definitions
  • (Functions)
  • Initialization: void setup()
  • Loop: void loop()

How to construct a sketch

Try to find examples for the sensors and actuators that you want to use. (Google is your friend.) For each of the examples, try to identify the above parts in the code. Try to execute each of the examples to test if you sensors and/or actuators work.

Debugging with Serial

In the right top corner of the Arduino IDE there is a icon to open the serial monitor, which allows you to see output produced by the Arduino. In order to used this, include the statement Serial.begin(9600); in void setup(). Now it is possible to use Serial.print() to print a string or the value of a variable. To add a newline at the end of the output, use Serial.println().

Combine examples

To combine two (or more examples) Sketches (progams) one need to copy parts of one of the sketches into another sketch. It is not possible to simply copy the whole sketch at the bottom of the target sketch. Instead all the parts of the sketch need to be copied one by one. The parts are:

  • Include statements for libraries
  • Pin definitions. If two examples, use the same pins, the need to be adjusted.
  • Functions, if any, can be placed in any order.
  • The code inside the setup() function. Usually, the order of the statements does not matter. (It is an idea to a short comment to before the part that you copied to help you remember where it came from.)
  • The code, if any, from loop() function, for reading a sensor, at the start of the target loop().
  • The code, if any, from loop() function, for controlling the actuators or displays, (usually) at the end of the target loop()

The order of the statements within the loop is important. It good to do the copying in small steps, and compile the sketch to see if there are any error messages. The most complicated part is (often) developing the decision code, which based on the sensor values, decide which actuators and/or displays need to be activated and which values should be used. For debugging, one can use Serial.print() statements.

Programming language

The programming language used in the Arduino IDE is based on C++. Some basic knowledge of this language is required to perform calculations and make decisions.

Variables

The language allows the definition of variables. A variable is a name of a memory location where a value of a certain type can be stored. Before a variable can be used, it needs to be defined. A variable definition consist of the name of the type, followed by the name of the variable, and optionally an equal sign (=) followed by a initialization expression. It is good practice to initialize every variable, because if a variable is not initialized it will contain some random value, which could lead to 'random' behaviour of the program.

The language contains the following types:

  • bool : containing a Boolean value, either true or false
  • char : containing a character. Ranging from -128 to 127.
  • unsigned char or byte : an integer value containing a value from 0 to 255.
  • int : containing an integer value ranging from -32,768 to 32.767.
  • unsigned int or word : containing a non-negative integer raging from 0 to 65535.
  • long : an integer value raging from -2,147,483,648 to 2,147,483,647.
  • unsigned long : an non-negative integer value raging from 0 to 4,294,967,295.
  • float or double : a floating point value.

Expressions

To calculate some value, expressions are used. Expressions consists of operands and operators. Operands are variable or constant value. The operators have a priority. Operators with a higher priority are executed before operators with a lower priority. Brackets can be used to force another execution order. Below the most important operators are defined in order of decreasing priority.

The monadic operators - (minus) and ! (not) have the highest priority. Monadic operators operate on the expression on the right. The minus operator inverts the sign of an integer of floating value. The not operator inverts a Boolean value. The remaining operators are diadic operators working on the expression on the left and the right.

The operators * (multiply), / (divide), and % (modulo) have the same priority. Thus the expression 4/2*3 evaluates to six because four is divided by two resulting in two, and this two is multiplied with trhee.

A multiplication can lead to an overflow, often resulting in a negative value. For example, multiplying to int values 10,000 will result in the value 100,000,000 which is outside the range of the type int. This problem occurs less with floating point values. When a multiplication involves an integer value and a floating point value, the result is a floating point value. This is also true for division. Division with two integer values will not result in a floating point value. So 8/3 will evaluate to the value two (2). The modulo operator returns the remainder after division. So 8/5 will return three (3).

The operators + (addition) and - (substraction) have the next priority. The result type is always the 'largest' of the types of the operands.

The next operators are the compare operators: == (equal to), != (not equal to), < (less than), > (larger than), <= (less than or equal to), and >= (larger than or equal to). The return a Boolean value.

The operator && is the logical 'and' operator, only returning true when both operands are true. When the operand of the expression on the left evaluates to false, the expression on the right is not evaluated.

The operator || is the logical 'or' operator, which returns true when one of the operands is true. When the operand of the expression on the left evaluates to true, the expression on the right is not evaluated.

The assignment operators require a variable as the left operand. The value of variable is changed after evaluation. The assignement operators are:

  • = : assigns the value on the left to the variable on the right.
  • *= : multiplies the value of the variable with the value on the right.
  • /= : divides the value of the variable with the value on the right.
  • += : adds the value on the right to the variable.
  • -= : substracts the value on the right from the value of the variable.

Statements

Links