How to join two projects on an Arduino

How to join two projects on an Arduino

Often even people with experience have problems with medium-sized projects, where they have no idea where the problem might be. Much less your solution. In this article, we will see an interesting way of joining two projects on an Arduino and how to find and avoid possible mistakes.

To exemplify, we will use a fictional character (Trixie) and a project hypothesis for her. Let’s assume that Trixie already knows the basics of Arduino, has already taken our course that comes with the Arduino UNO Beginner / Basic Kit and now wants to do his own project to solve a problem at home.

The project

Trixie wants to make an automatic mini-gate using the Arduino. She scribbled on a piece of paper what her gate should be like and saw that she would need something to detect the presence of the car and something to move the gate.

For presence detection, she decided to use an Ultrasonic Distance Sensor HC-SR04 . For the move, she knew that a small servant would be sufficient. She chose the SG90 9g Tower Pro 360º Servo Motor , which was already in the kit she had purchased. Once the components were specified, she already had the list of what she would need:

  • Arduino Uno ;
  • Micro Servo 9g SG90 ;
  • Ultrasonic Distance Sensor HC-SR04 ;
  • Male-Male Jumpers ;
  • 400-point breadboard .

To do this project, she saw class 6 of the course that explains how the servo motor works with the Arduino, put the ultrasonic sensor together as she teaches it and … Nothing worked. And now?

Divide to conquer

As Emperor Julius Caesar used to say “ divide and rule ”. When you want to join two components in a single project, it is interesting to separate each one well and understand them separately. The ideal is to assemble a separate project for each of the components and join the two projects on an Arduino.

It is a good practice to master both electronic assembly and programming. Once you have mastered how the component connection works with Arduino – if you use an analog, digital, PWM or some type of communication – and programming is done – how to start the component, which changes when changing the assembly, which parameters are used to control it and what those parameters represent – is that it is advisable to put the components together.

In the case of Trixie, it is easy to divide the problem, it is a sensor (the HC-SR04 ) and an actuator (the micro servo ). Sometimes it is more complex and the division is not always so visible, but always try to divide it into smaller problems. Solve them separately, master each one of the elements, and then join in a unique solution.

Mastering each element

Once divided, to join two projects on an Arduino, it is interesting to study them separately. As we saw earlier, Trixie saw that it had classes that taught how the two components work, our suggestion is to master each one separately, see how the components are connected and how their programming works on Arduino.

I understand the servo motor

understanding servo motor

As we saw in the servo motor class , the servo motor is connected to the Arduino by three wires, the red one for 5V, the brown one for GND, and the orange one for some analog port (which has the tilde ‘~’ on the side of the number). It is important to know what the component needs to be triggered.

In the programming part, it is interesting to see the libraries used and how the engine is started. Below we have a schedule similar to the course.

// Project 8 - Starting an Engine

#include <Servo.h>

Servant myServo;

int button = 7;

void setup () 

{

  pinMode (button, INPUT_PULLUP); // set the button to exit

}

void loop () 

{

  myServo.attach (9);

  if (digitalRead (button) == LOW)

  {

    for (int angle = 0; angle <= 180; angle ++) // Increase the angle until it reaches 180 degrees

    {

      myServo.write (angle);

      delay (10);

    }

    for (int angle = 180; angle> = 0; angle -) // Decrease the servo angle

    {

      myServo.write (angle);

      delay (10);

    }

  }

  myServo.detach ();

}

Note that it is necessary to include the Servo.h library at the beginning of the code, as it is already installed with the IDE. It is also necessary to declare the servo motor, as done in this line “Servo meuServo;”

Within the loop, it is necessary to “turn on” the servo with the command “meuServo.attach (9);”. It is also possible to see inside the code how the command for positioning the servo is made, in the line “meuServo.write (angle);”.

Once the code is understood, we can reduce it to the minimum necessary to make it work. We removed the button from the circuit and the if and for structures. Try to do this, even if mentally, the code should be close to this program that just puts the servant in one position, waits for a little and puts it in another.

// Program: Minimal servo motor

#include <Servo.h> // Includes the library

Servant myServo; // Declares the servo motor

void setup () {

  // We don't need anything here

}

void loop () {

  myServo.attach (9); // Turns on the servo motor

  myServo.write (0); // Position the engine at 0

  delay (1000); // Wait 1 second

  myServo.write (45); // Position the engine at 45 degrees

  delay (1000); // Wait 1 second

  myServo.detach (); // Turn the servo motor off

}

See the comments in the code to help you understand the program. It’s always a good idea to comment on your code so that you can understand it yourself in the future.

Given how the connections and programming of this project are made, we can say that we have mastered the functioning of the servo motor and can move on to the next step.

Understanding the ultrasonic sensor

understanding sensor

Trixie even saw the operation of the Ultrasonic Sensor HC-SR04 in class 9 of the course , but did not make an analysis of its operation when joining the two projects in one Arduino.

As seen in the article, the sensor is connected as follows:

We can see that the Arduino power pins (5V and GND) and two digital ports are used.

// Program: Connect the HC-SR04 Ultrasonic Sensor to Arduino

// Load the ultrasonic sensor library

#include <Ultrasonic.h>

// Defines the pins for the trigger and echo

#define pino_trigger 4

#define pino_echo 5

// Starts the sensor on the pins defined above

Ultrasonic ultrasonic (pino_trigger, pino_echo);

void setup ()

{

Serial.begin (9600);

Serial.println ("Reading sensor data ...");

}

void loop ()

{

// Read the sensor information, in cm

cmMsec float;

long microsec = ultrasonic.timing ();

cmMsec = ultrasonic.convert (microsec, Ultrasonic :: CM);

// Displays information on the serial monitor

Serial.print ("Distance in cm:");

Serial.print (cmMsec);

delay (1000);

}

In the code we can see, as with the servant, the use of a library, in the line “#include <Ultrasonic.h>”. In this case, however, it is necessary to install it by downloading the file in .ZIP format and adding it to the Arduino IDE. To do this, go to “Sketch -> Include Libraries -> Add Library .ZIP” and select the file.

After adding the library, the sensor is started, with the command “Ultrasonic ultrasonic (pino_trigger, pino_echo);”. The serial port is also started, which is not mandatory but is a great way to understand the sensor and find problems in the code logic.

Once the sensor is started, it is necessary to use a variable to read the data, in the program it is called “ microsec ” and receives the value of the function “ ultrasonic.timing () “. The data is read in the line “ cmMsec = ultrasonic.convert (microsec, Ultrasonic :: CM); ”Where the“ cmMsec ” variable receives the value read and then is sent to the serial. Without sending the value to the serial, another way of knowing the value read would have to be put.

Put the two projects together on an Arduino

Joining the program

Once the functioning of the two components is understood, it is time to join the two in a project using an Arduino. Once again, divide to conquer. Let’s see how the circuit is assembled with the two components and then how to join the two programs.

Join the circuits

When we studied the components separately, we saw the pins that each component needs:

Servo motor

  • 5 V
  • GND
  • Signal (analog output)

Ultrasonic sensor 

  • 5 V
  • Echo (digital port)
  • Trig (digital port)
  • GND

In the circuit with the two components together, we can use the same 5 V and GND pin for both components. In addition to these two Arduino pins, we need two more for the ultrasonic sensor, which we can keep the same ones seen previously (4 and 5). For the servo motor, any analog output could work, in this case we kept the one used before (9).

Joining the programs

Analogous to what was done with the circuits, to join the two programs in a single Arduino you must pay attention to what really needs to be placed and what cannot be repeated, we cannot just copy and paste the two programs together and hope that it works .

An Arduino program has two main structures ” setup () ” and ” loop () “, as seen in Lesson 2 of the course .

setup () – Part of the program in which the initial program options are configured: the initial values ​​of a variable, whether a port will be used as an input or output, messages for the user, etc. This function is performed only once when the Arduino is turned on.

loop () – Unlike the setup () function, this part of the program repeats a command structure continuously or until some “stop” command is sent to Arduino. Let’s see exactly how this works over the course of the projects.

It turns out that in an Arduino program you can only have one of each, so you must choose what is needed from each of the codes and put it in the ” setup () ” and ” loop () ” according to what we saw when we studied the codes.

In short, the program positions the servo motor in a certain position (45º) and sends “Open” to the computer when there is something close to the ultrasonic sensor (less than 5 cm). If there is nothing nearby, put the servant in another position (0º) and send “Closed”.

// Program: Automatic cancellation

// Author: Electrofun

// Loads the ultrasonic sensor and servo libraries

#include <Ultrasonic.h>

#include <Servo.h>

// For the ultrasonic sensor

// Defines the pins for the trigger and echo

#define pino_trigger 4

#define pino_echo 5

// Initializes the sensor on the pins defined above

Ultrasonic ultrasonic (pino_trigger, pino_echo);

// For the servo motor

Servant myServo; // Declares the servo motor

void setup ()

{

// We put the serial to check for possible errors

Serial.begin (9600);

Serial.println ("Reading sensor data ...");

}

void loop ()

{

// Read the information from the ultrasonic sensor in cm

cmMsec float;

long microsec = ultrasonic.timing ();

cmMsec = ultrasonic.convert (microsec, Ultrasonic :: CM);

myServo.attach (9); // Turns on the servo motor

// You can change this value according to the desired operation

if (cmMsec <5) // Check if the sensor value is less than 5

{

// If the value is

myServo.write (45); // Position the engine at 45 degrees

delay (1000); // Wait 1 second

Serial.println ("Open"); // Sends message via serial

} else {

// If no

myServo.write (0); // Position the engine at 0

delay (100); // Waits 0.1 second

Serial.println ("Closed"); // Sends message via serial

}

myServo.detach (); // Turn the servo motor off

// We put the distance in the serial to adjust the sensor

Serial.print ("Distance in cm:");

Serial.println (cmMsec);

}

Note that at the beginning of the program we declare the two necessary libraries. Inside the ” setup () ” it was not necessary to put anything, but we started the serial port to help us find errors in the program.

In the ” loop () ” we define the behavior of the project. At the beginning of the loop we read the measurement of the ultrasonic sensor, we compare the measurement with the value 5. This value can be changed according to your sensor and how you want the behavior. It is much easier to adjust this value when accompanying the sensor measurement through the serial. If the measurement is less than the value, we place the servant in a position; if it is bigger, we put it in another position.

Conclusion

Now that Trixie has already managed to combine the servo motor and the ultrasonic sensor in a single Arduino, she can worry about the other things in her project. It is worth remembering that before joining two projects on an Arduino she wanted to make a gate for cars.

Leave a Reply

Your email address will not be published. Required fields are marked *