Before leaping into programming, it might help to explain what an Arduino is. It's a family of small electronics boards containing a microcontroller plus the wherewithal to: power it; manage a serial connection to a Personal Computer; and allow a program to be installed. It comes with hardware and software designed to simplify using the microcontroller. There are a number of different Arduino models, the Uno is the work horse of the family and easiest to experiment with.
A microcontroller is a tiny computer designed to interface with other electronics. They don't do general purpose computing like a PC. Typically a microcontroller is kept so simple it doesn't have an operating system and it only runs one program dedicated to a single purpose. The program starts when the machine is turned on, and it stops only when switched off. They are idea for monitoring sensors, including internal timers, and controlling the electronics used to manage lights, motors, valves, servos, heaters or whatever. The inputs might include a simple keyboard, or just a few switches, and the outputs might include a simple display, or perhaps just a few LEDS.
A microcontroller program runs in a permanent loop, allowing forever the logic: 'if an INPUT then calculate an OUTPUT'. Within the loop it's possible to read inputs, detect events, store information, perform arithmetic and logical operations, make comparisons, and write outputs. The program is a list of instructions followed one after the other, except there are instructions that can, optionally, cause the computer to jump to elsewhere in a sequence.
Consider a problem like a traffic light controller at a 4-way junction. There are specific rules to be followed rigorously: only one signal may be green at a time, never two or more together! To keep traffic moving each road must get a green signal in turn, and the green signal must stay on for a useful length of time. To warn slow-reacting drivers lights go from red to green via a short amber, and – because they need time to stop – lights change from green to red after a longer amber period. A simple system might give each road an equal amount of green time, but a real world controller would more likely adjust go time depending on the proportional number of cars waiting on each road. To do this, the microcontroller has to keep count of cars detected by sensors, and recalculate green time as necessary. In the event of a fault, the system should restart automatically, starting with all roads at red. All this is getting a little complicated, especially if the system has to allow for filter lanes, or another nearby signal controlled junction, or has a manual override, or is only activated during heavy traffic, or reports traffic density to a control centre, or is sometimes managed remotely, or detects headlamps at night so cars aren't stopped unnecessarily.
Quite a design challenge, but most of this can be done expensively with relays and mechanical timers. Later, semi-conductors and integrated circuits provided cheaper and more reliable equivalent circuitry. Today, a £5 microcontroller can do all this and more, though it has to be said that designing fail-safe control systems like traffic signals or fly-by-wire aircraft is not simple!
The extra power of microcontrollers can be used to improve all manner of mechanical systems. For example, a car engine's timing, fuel quantity, and fuel mix can all be changed depending on air and engine temperatures, knock detection, engine load, humidity, air pressure and exhaust content. At the press of a button the driver can optimise for fuel economy or performance – rather than needing a mechanic to retune the engine in a garage, a microcontroller can do it in motion.
Microcontrollers can be programmed to manage almost any electro-mechanical machine, and programs can be upgraded to fix bugs or add new features without having to rewire anything.
The microcontroller used by the Arduino Uno isn't particularly powerful, but it has 20 pins that can used for input or output and enough oomph to manage a wide range of basic and not so basic control requirements.
In the case of a stepper motor, turning the motor is easy. The pin connected to ENA+ is declared an OUTPUT, and set HIGH. This tells the black box to obey pulses. Then the pin connected to DIR+ is declared an OUTPUT and set to HIGH or LOW depending on the required direction. The controller adjusts it's output to turn the motor either clockwise or anticlockwise. Finally the pin connected to PUL+ is declared an OUTPUT and sent a sequence of HIGH LOW HIGH LOW HIGH LOW signals. Each HIGH steps the motor once. With this hard coded in the program the motor would run in one direction forever.
Mostly though the user wants to stop and start the motor on command, and decide which direction it turns, and determine how many steps the motor will take, and the rpm. And perhaps automatically stopping the motor if it hits an end stop. Achieving this involves declaring other pins as INPUTS, connecting switches, and then writing code to detect if a switch is ON or OFF.
Hope that makes sense!
Dave
Edited By SillyOldDuffer on 17/02/2019 19:23:21