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.

Difference between revisions of "HowTo simple Arduino project"

From

Jump to: navigation, search
(Added some advice on writing a sketch)
(Combine examples)
Line 140: Line 140:
 
* Pin definitions. If two examples, use the same pins, the need to be adjusted.
 
* Pin definitions. If two examples, use the same pins, the need to be adjusted.
 
* Functions, if any, can be placed in any order.
 
* Functions, if any, can be placed in any order.
* The code inside the <code>setup()</code> function. Usually, the order of the
+
* The code inside the <code>setup()</code> 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.)
  statements does not matter. (It is an idea to a short comment to before the
+
* The code, if any, from <code>loop()</code> function, for reading a sensor, at the start of the target <code>loop()</code>.
  part that you copied to help you remember where it came from.)
+
* The code, if any, from <code>loop()</code> function, for controlling the acturators or displays, (usually) at the end of the target <code>loop()</code>
* The code, if any, from <code>loop()</code> function, for reading a sensor,
+
  at the start of the target <code>loop()</code>.
+
* The code, if any, from <code>loop()</code> function, for controlling the
+
  acturators or displays, (usually) at the end of the target <code>loop()</code>
+
  
 
The order of the statements within the loop is important. It good to do the copying
 
The order of the statements within the loop is important. It good to do the copying

Revision as of 14:05, 6 June 2018

Simple Arduino sensor & acturator 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. Acturators (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 acturators 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:

Acturators

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 acturators
  • 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 accurators

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

== How to construct a program

Try to find examples for the sensors and acurators 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 accurators 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 acturators 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.

Links