With all the basic stuff out of the way, now it’s time for the fun part – uploading your first program!
Ensure that your Arduino is connected to your PC using the CP2102 (refer to this if you don’t know how).
Now start up the Arduino IDE and choose Examples -> 01. Basics -> Blink.
This program is excellent for verifying the connecting to the computer, so choose the port that your Arduino is connected to (refer to this if you don’t know how or cant see the port).
Now hit the upload button. Hopefully you should see the small internal LED on your Arduino board blink for a second and then turn off for a second.
If you do, congratulations, you have just uploaded your first program successfully!
If you don’t, you may have to check the port in the Tools -> Port menu and if this is grayed out or it doesn’t show the port you have connected the Arduino to, you may have the Arduino IDE Driver problem.
You can play around in the blink program to change the blink interval. Try changing the delay to 200 instead of 1000 milliseconds and upload it again.
Example project explained
So, let’s have a look at one of the more advanced programs.
The setup() method is running only once when the Arduino starts up so this is where you initialize stuff. After running the setup() method, the loop method will start running and wont stop until you shut down the Arduino (pull the power from the Arduino or put the Arduino into Deep sleep).
In the program above the Serial.begin(9600) initializes the Serial communication, so after this you can print out text to the Serial Monitor. Remember to set the baud rate in the Serial Monitor to the same baud rate as in the program or you will see garbage characters.
Serial.begin() should always go in to the setup() method.
Now in the loop() method you can start printing out text to the Serial Monitor using Serial.print(“Example text”) or Serial.println(“Example text”) if you want line break after your outputted text.
Another important point here is the delay(300) in the last part of the program. This will tell to program to pause for 300 milliseconds and then continue. Use this whenever you want something to for example stay on for a little while and then turn off.
Going a bit deeper in the program, you can use analogRead() to read from an analog port and digitalRead() to read from a digital port (see here if you don’t know about analog/digital ports). This is used to read the state of for example a gas sensor – what is the current level of gas in the room…
Similarly you can use analogWrite() and digitalWrite() to write to a port. A simple example to use digitalWrite() is when you want to switch an LED on or off. Then you just write digitalWrite(2, HIGH) to turn whatever you have connected to digital pin 2 on the Arduino on and digitalWrite(2, LOW) to turn it off.
When using pins for either reading or writing, you should always remember to initialize whatever pins you are using. This is done in the setup() section.
You initialize the pins by inserting pinMode(2, OUTPUT) if you want to write to pin 2 and pinMode(2, INPUT) if you want to read from pin 2 in your program.
Awesome, you are now ready for a more advanced program!
Checkout some of my basic example projects…