Arduino is open hardware platform that became extremely popular due to its simplicity, free and friendly development environment and massive number of ready to use examples and libraries that can be used to control most of the popular electronics components, like displays, sensors, motors, servos, etc. Most popular Arduino boards are equipped with 8 bit Atmega processors. It does not look impressive nowadays, but when programmed with care this units can really do much. For example most of amateur 3D printers are controlled with such processor. And Arduino may also serve very well astronomy amateurs with their every night tasks.
To start your adventure with Arduino you need to have an Arduino board (like the one in the image above) and Arduino IDE, where you will write your code (https://www.arduino.cc/en/Main/Software). You may start with Arduino Uno, but if you prefer something smaller you may look for Arduino Nano. And if you need more outputs then Arduino Mega may be a good choice. Arduino board may be powered directly from computer USB port, so after attaching board to PC with USB cable you may start to code.
In the Arduino IDE there are many examples that you may take a look into. Each Arduino program must contain two blocks: setup and loop. The first one is performed only once, after Arduino reset or power up. The code inside loop block is performed constantly over and over. In the simple example below in the setup block digital pin number 2 is set as output, and then its state is set to low. Then in loop block pin 2 is toggled between low and high states every 1000 milliseconds.
void setup() {
pinMode(2, OUTPUT);
digitalWrite(2, LOW);
}
void loop() {
digitalWrite(2, HIGH);
delay(1000);
digitalWrite(2, LOW);
delay(1000);
}
When you connect LED diode with resistor (1 kiloohm) in series to pin 2 of Arduino board, you can see it will be blinking. For development purposes it is quite convenient to purchase also a breadboard with connecting wires, so you can build up and test even pretty complicated circuits.
Next example, that could be more useful will be relay controller, that can switch on of off one or more relays remotely. After connecting to computer, you may send commands over the serial port to control relays. You cannot however connect relay directly to Arduino pins (except for 5V low power relays), so you need additional ULN2003 or similar chip, or use Arduino relay shield, like the one below:
Arduino shields are printed boards that can be put onto Arduino board like a sandwich. Relay shield contains four relays that can be used to any purpose – you may switch power to any device like camera, mount, flat box, or you may also use it to control DSLR camera shutter.
Take a look to the code below. At the beginning you define pins that control relays – pins 4 to 7 are usually used in relay shields that contain four relays. Then two additional variables are declared – inputString that holds input command that comes from serial port. And second one stringComplete is a flag that is set when command was send (new line character has arrived) and can be parsed and processed. In setup block we set all relays states to OFF at the beginning, then we initialise Arduino serial port and reserve some memory to hold input commands. In loop block we check all the time if stringComplete flag is set, and when it is (the command arrived) we parse it and set proper relay state to ON or OFF. After that we clean string variable and also reset stringComplete flag.
There is another method in the code called serialEvent. It comes from Arduino framework and is called each time when data arrives to serial port. In this method we read serial port data byte by byte, store it in inputString variable, and when new line character is sent we set stringComplete flag, so the new command will be processed in next loop block iteration.
#define RELAY_A_PIN 4
#define RELAY_B_PIN 5
#define RELAY_C_PIN 6
#define RELAY_D_PIN 7
String inputString = ""; // variable that holds serial port command
boolean stringComplete = false; // flag that indicates command end
void setup() {
// we set relay state to OFF
digitalWrite(RELAY_A_PIN, LOW);
digitalWrite(RELAY_B_PIN, LOW);
digitalWrite(RELAY_C_PIN, LOW);
digitalWrite(RELAY_D_PIN, LOW);
// initialize serial port
Serial.begin(9600);
// book 5 bytes for input command
inputString.reserve(5);
}
void loop() {
// if command was received
if (stringComplete) {
// we parse it: (A:0, A:1, C:0)
byte relayPin = 0;
if(inputString.charAt(0) == 'A')
relayPin = RELAY_A_PIN;
else if (inputString.charAt(0) == 'B')
relayPin = RELAY_B_PIN;
else if (inputString.charAt(0) == 'C')
relayPin = RELAY_C_PIN;
else if (inputString.charAt(0) == 'D')
relayPin = RELAY_D_PIN;
if(relayPin > 0) {
// and its argument
if(inputString.charAt(2) == '0') {
digitalWrite(relayPin, LOW);
} else {
digitalWrite(relayPin, HIGH);
}
}
// clear command buffer
inputString = "";
stringComplete = false;
}
}
/*
* SerialEvent is triggered every time when new data
* is received at serial port
*/
void serialEvent() {
while (Serial.available()) {
// we fetch one byte
char inChar = (char)Serial.read();
// add it to variable that holds the input command
inputString += inChar;
// if received character is new line char
// we set the flag that indicates command received complete
if (inChar == '\n') {
stringComplete = true;
}
}
}
When you upload sketch to Arduino it is ready to receive command. You will need any serial port terminal to send commands (like Termite or RealTerm in Windows). Example commands (each needs to be ended with new line character):
A:1 - switch first relay ON
C:0 - switch third relay OFF
You can also write some scripts that will send proper commands to serial port, or write simple graphical application that will control it. And that it is. Now you can switch remotely using your computer any device in your setup.
If you do not want to use relay shield, but would rather like to wire components by yourself here is simple schematic for this example (only one channel for pin 4 is shown, however with ULN2003 unit you can control up to 8 relays):
In the next tutorial part we will get familiar with sensors and Arduino libraries plus we use LCD to display some data. More fun to come!! 🙂
Clear skies!
Cover photo by Frank Wang on Unsplash