Fun with Arduino - a Series of Introductory Videos [external source]

Fun with Arduino - Introductory Videos

As a model railway hobbyist I have found the Arduino to be an invaluable tool. It can liven up any layout by making things move, switch lights on and off, and it can even be used as a full fledged DCC decoder.

Not every hobbyist has affinity with electronics or with software however. To some the hill to start can be just too steep, even while there may be an interest to use Arduino and to try things out.

That is where the plan arose to do a series of articles and videos titled 'Fun with Arduino' ... aimed at anyone who is not an IT specialist (yet :slight_smile: ).

The first two articles, both with video, are out:

Fun with Arduino 01 Getting Started in 6 Easy Steps.

Fun with Arduino 02 Digital Input and -Output

Have fun.

I enjoyed both of those, and wonder what you plan for future episodes?

I'm asking because there is no shortage of Arduino material around the webz. In fact it sometimes seems there's too much, some of it subtly different from others, some of it conflicting and some of it factually incorrect.

Apart from some factual faults like suggesting to power servos from the Arduino 5V, the examples on this site are as good a place as any to start. What's going to make your series different from the others?

@Willpatel_Kendmirez
Yes, many articles and video's are already available. Yet at our model railway club I found the hurdle still is steep for many to start, even 'beginner' tutorials tend to go too fast. I intend to intermingle a bit more explanation on hardware and software, hoping that non electronic / IT educated might be able to follow along.

Future subjects will probably focus on examples related to model railway lauouts: lighting (LED and relay / FET), servo (railway crossong), optical sensor (automate the railway crossong), DC or stepper motor to introduce some movement ... things like that.

On Rudy's Arduino Projects blog I'll probaby not promote powering the Arduino directly via a 5V pin, whereas on my Rudys Model Railway blog I do. If 5V is the only available power on the railway layout there's no choice. What is the 'factual faiult' you mention?

RudyB:
What is the 'factual faiult' you mention?

That one shouldn't power servos from the Arduino 5V as a matter of good practice... ok, maybe not factually incorrect as long as it's a small servo but even micro servos can draw 7-800mA. Both the servo sweep and knob examples say to do that... as soon as one moves to more than one and / or a bigger servo there's not enough current.

(I didn't say not to power an Arduino into its 5V pin, I said not to power anything substantial out of it.)

I like your idea of making your material "Arduino in a model railway world": that makes perfect sense for your audience, who do not want to get bogged down in the arcana of electrical engineering, but do want to get their model railways to work safely and reliably without too much fannying about.

Coincidentally, Robin2 had this to say to me yesterday:

Robin2:
I use them [nRF24L01's] for radio control for model trains. I have made some units for my model railway club that take the place of wired controllers so folk aren't tripping over the wires. And I have some installed in my own trains for battery powered radio control.

The third video is online.

To control the on board LED is fun, but the real fun only starts when we can control external LEDs, like on a switch panel, or on a model railway layout. This video is about how to connect a LED and how to choose the value of the series resister we need to set the LED brightness.

In the next video we are going to control the external LED and we will simulate a night cycle on a model layout.

Link to Fun with Arduino 03 External LED and Resistor

The goals in this video are:

  • To make our code better readable and easier to maintain.
  • We imagine pin 8 operates 20 street lights on a model railway layout. They switch on when we press the button. To simulate a night cycle we want them to stay on for 3 minutes and then automatically switch off again.

Link to Fun with Arduino 04 Readable & Maintainable Code , #define, delay()

Arduino outputs can only switch 20mA. If we want to control groups of multuple lights on our model railway layout, we will need some more 'oompf'. A 12V power supply will do great and we can use Relay or FET modules, controlled by the Arduino, to swicth the higher current / voltage. How to do this, how to wire this, is the subject of this video and article.

Link to Fun with Arduino 05 Connect multiple LEDs with a Relay or a FET

In this 6th video we are going to create a fully automatic day / night sequence, witch a toggle switch and an indicator LED on our switch panel. The day & night times are going to be configurable in seconds, which means we need some math to go to milliseconds.

Link to Fun with Arduino 06 Automatic Day & Night Cycle with on/off Switch

Our day / night module of the previous video works perfect, but it controls just one light group. On our layout we probably have multiple groups ... houses, street lights, a railway station or an industry area. In this video we're going to see how we can control multiple light groups in a day / night cycle, while of course they do not switch all at the same time and also while making it non predictable.

Link to Fun with Arduino 07 Day & Night Cycle, Multiple Light Groups, Random Times

Our way of modifying the day / night cycle time seemed quite handy, but we do need to modify the code and upload it again every time we like to change the cycle time. Is there maybe an easier way?

Yes there is ... we can change the cycle time 'on the fly' via a User Interface. There are different solutions, with hardware otr with software. We're going to try them both. In this video we'll do the first preparations, writing text and numbers to the PC screen via Serial.print().

Link to Fun with Arduino 08 User Interface Serial.print()

Before we continue to work on our User Interface, let's first take a moment to have a closer look at variables and data types. We are going to use ever more variables in the coming videos ... and ... we'll have a look at a pitfall concerning data types that prevented our code from previous video 8 to always work as intended.

Fun with Arduino 09 Variables byte int long unsigned

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).

...R

Now that we have our code working, by using the correct data type or by typecasting, we can continue the work on our User interface. The goal in this video is to display the status of our day / night cycle on screen ... is it switched on or off, is it day or night, and what is the currently used day / night time. All this will be dynamically updated.

Fun with Arduino 10 Show Cycle Status and Time with Serial.print

The User Interface works, it shows us the status if the day / night cycle on screen. We're now going to add the option to change the cycle time via the keyboard. The functions we are going to use are Serial.available(), which tells us that there is new input, and Serial.read() or Serial.parseInt() to read the characters that are typed.

Fun with Arduino 11 Keyboard Input via Serial Read and ParseInt

Now that we can change the cycle time via the PC keyboard, let's have a look at a hardware oriented solution ... a rotating knob. We connect a potentiometer to an analog input and read the voltage with the analogRead() instruction. With the map() instruction we can convert the range from 0-1023 to the range that we like to use for our cycle time, like say 1-9 minutes with a 1 minute step size, or maybe 10-300 seconds, with a 10 second step size.

Fun with Arduino 12 Analog Input, analogRead(), Change Range, map()

The delay() statement that we used so far for our timing stalls the Arduino. This leads to a complete lack of feedback when we change the cycle time while the cycle is running. Luckily there is a solution: we can use the Arduino internal clock, which counts milliseconds from the moment the Arduino is started. We can read the clock using the millis() statement and we can decide if it is time for action.

Fun with Arduino 13 Timer with millis(), no delay(), Multitasking

Now that we know how to get rid of the delay(0 and use millis() in stead (video 13) we can finalize our Automatic Day Night Light Cycle unit to have direct on screen feedback of cycle time adjustment by the user and to have the cycle stop, and the lights turn off, immediately when the switch is set to ‘off’.

Our unit has quite nice specifications:

  • Configurable timing, via keyboard or via analog input with on screen display
  • An option to randomize the times to give it some ‘livelyness’
  • On screen display of the on/off, day/night state and the cycles times

Fun with Arduino 14 Day Night Cycle with millis(), no Delay, Direct Feedback

We used analogRead() to read the voltage on our potentiometer. The Arduino also has the opposite instruction: analogWrite(). This name is somewhat misleading. Unlike with an analog input, where a 10 bit A/D converter is used, the Arduino does not have a D/A converter on board.

The analogWrite() function uses a technique called Pulse Width Modulation. A digital output switches between HIGH and LOW in a fast pace, whereby the HIGH percentage is proportional to the analog value we wish to send out. If a device that receives the signal is too slow to follow the switching frequency, the result is it 'sees' the average of the on/off times. This also holds for light ... even though LEDs are fast enough to follow the switch frequency, our human eyes + brain are not and we see an average brightness.

Fun with Arduino 15 LED Dimmer, analogWrite(), Pulse Width Modulation

Fun with Arduino 16 LED Dimming with Fade, analogWrite(), millis()

Now that we know how to dim LEDs with analoWrite(), we can go a step further and change the dimming over time to create a gradual fade in or out. This is a nice effect for instance for LED strips mounted under kitchen cabinets, or for LED strip overhead lighting on a model railway layout to simulate a gradual change from night to day. And also for the red/green transition of railway signals along the track a fade gives just that little extra eye candy.

Fun with Arduino 16 LED Dimming with Fade, analogWrite(), millis()

We're at the start of a new Arduino project: an automatic railway crossing.

The system comprises several parts: train detection (optical), blinking lights ('blink' with a twist), a moving beam (servo motor).

We'll look into a way of specifying these kinds of systems as well as a way to translate the specifications into code, with a stepwise approach that does not put too much strain on our grey cells.

Fun with Arduino 17 Railway Crossing, State Transition Diagram, switch()