Introduction: Arduino at Heart Littlebits Quick Start
This instructable will enable you to code a simple digital and analog circuit as well as provide a starter sketch that you can use to get started quickly and also to debug your circuit. This Instructable is geared towards beginners and parents, the instructions should give you enough to get started in no time.
Step 1: Initial Setup
First, you will need to have the Arduino IDE downloaded from https://arduino.cc/download and running on your computer.
Next, you need to download the Quick Start Sketch from Github
https://github.com/ricklon/littlebitsatheart
There are two sketches we can use in the sketch folder:
The startersket.ino is a sketch that turns the Arduino into a "superbit" buttons and sliders on one side trigger a corresponding output action on the other.
The starterdebug.ino is a sketch the uses the USB serial cable to communicate to the Arduino IDE serial terminal. Once it is loaded you can view the serial terminal and see what values are being input to the Arduino and then read the values that are sent to the output side of the Arduino. This way you can tell if your input bits have power and are giving you the correct information. As well as, being able to tell if the output side of the Arduino sending the right kind of signals to the connected bits.
All of the getting started examples use can be done with either starter sketch. However, in each step we will show the minimum sketch for each example.
For this quick tutorial, you will need the following littleBits:
Starter Sketch Code:
//declare Inputs //lower case a0, d1 is not defined. d0 is not defined. int valA0; int valA1; int valD0; void setup() { //pin mode OUTPUT data or energy out pinMode(1, OUTPUT); pinMode(5, OUTPUT); pinMode(9, OUTPUT); //pin mode INPUT data or energy in pinMode(0, INPUT); //usually you don't need to set analog pinmode because they always take data in } void loop() { //get Inputs //lower case a0, d1 is not defined. d0 is not defined. valA0 = analogRead(A0); valA1 = analogRead(A1); valD0 = digitalRead(0); //interact with world //scale the values for the proper OUTPUT //do not have to scale digital INPUT D0 int mapA0 = map(valA0, 0, 1023, 0, 255); int mapA1 = map(valA1, 0, 1023, 0, 255); //send Outputs digitalWrite(1, valD0); analogWrite(5, mapA0); analogWrite(9, mapA1); }
Step 2: Identify Digital and Analog Components
We have two different types of signals: Digital and Analog. Digital signals are ON/OFF, HIGH/LOW or TRUE/FALSE signals. These digital signals only have two states. Analog signals can be ON/OFF, HIGH/LOW and everything in between. While analog components can handle digital signals like TRUE/FALSE, digital components will not know what to do with analog signals that are between HIGH and LOW. Many real world situations are gradations between HIGH and LOW.
Besides keeping the different types of signals in mind, you will also need to keep a clear idea of which are input and output components. These are important factors in determining how you hook them up to the Arduino.
Input components and their corresponding type:
- Button i3 (digital)
- Sound Trigger i20 (digital)
- Pulse i16 (digital)
- Timeout i17 (digital)
- Slide Dimmer i5 (analog)
Output components and their corresponding type:
- Buzzer o6 (digital)
- Light Wire o16 (digital)
- Servo motor o11 (analog)
- RGB LED o3 (analog)
- DC motor o5 (analog)
On the Arduino w6, note the pin types for both the input pin and the corresponding output pin:
- D0 (input) / d1 (output) - Digital
- A0 (input) / d5 (output) - Analog
- A1 (input) / d9 (output) - Analog
*Note: The input pins must be coded with a uppercase letter instead of the lowercase notation which may cause an error in compiling.
Now, that you know the basics, let's start coding!
Step 3: Build a Simple Digital Circuit
You will need the following components:
- Power (p1) with cable and 9V battery
- Sound Trigger (i20)
- Arduino (w6) with Micro USB cable
- Light Wire (o16)
Connect the Sound Trigger to d0 connector on the Arduino (w6).
Connect the Light Wire to Pin d1 (directly across from Pin d0)
Next connect the Power to the Sound Trigger.
Turn on the power. What happens when you clap to activated the Sound Trigger? The Light Wire should turn on but only for a moment.
We will program the Arduino to keep the light on until you clap again.
Step 4: Program Simple Digital Circuit
Load the basic starter sketch at https://github.com/ricklon/littlebitsatheart/tree/master/sketchbook/startersketch or use the alternative starter sketch listed at the end of the tutorial. The sketch below only declares a variable valD0, adjusts the d0 to be an INPUT, reads the state of d0, and then writes the value of valD0 to the output.
Modify the starter sketch so that it looks like the sample code below:
int valD0; void setup() { //pin mode INPUT data or energy in pinMode(0, INPUT); //pin mode for OUTPUT pin to send energy out pinMode(1, OUTPUT); } void loop() { //get Inputs //lower case a0, d1 is not defined. d0 is not defined. valD0 = digitalRead(0); //send Outputs digitalWrite(1, valD0); }
Step 5: Build a Simple Analog Circuit
For this circuit, you will need the following littleBits:
- Arduino (w6)
- Slide Dimmer (i5)
- Power (p1) with 9V battery cable
- RGB LED (o3)
Connect the Slide Dimmer switch to the a0 pin on the Ardunio.
Then connect the RGB LED to the d5 pin.
Attach the Power module to the Slide Dimmer with 9V battery connected.
Plug in microUSB cable into the Arduino and computer.
Now you are ready to code!
Step 6: Program Analog Circuit
Load the basic starter sketch at https://github.com/ricklon/littlebitsatheart/tree/master/sketchbook/startersketch or use the alternative starter sketch listed at the end of the tutorial. The sketch below only declares the variable valA0, configure the pinmode for sending data out, and then reads the analog value of pin A0. The values that analogRead can return are between 0 and 1023. The code then stores the value in valA0. valA0 is then written to the
Modify the starter sketch so that it looks like the sample code below:
//declare Inputs //lower case a0, d1 is not defined. d0 is not defined. int valA0; void setup() { //pin mode OUTPUT data or energy out pinMode(5, OUTPUT); //usually you don't need to set analog pinmode because they always take data in } void loop() { //get Inputs //lower case a0, d1 is not defined. d0 is not defined. valA0 = analogRead(A0); //interact with world //scale the values for the proper OUTPUT //do not have to scale digital INPUT D0 int mapA0 = map(valA0, 0, 1023, 0, 255); //send Outputs analogWrite(5, mapA0); }
Step 7: A1 Configuration Sample Sketch
Below is the A1 Sketch should you chose to use the other set of analog pins instead of A0. Note that it is essentially the same as the A0 Sketch except that all the A0 references have been changed to A1.
//declare Inputs //lower case a0, d1 is not defined. d0 is not defined. int valA1; void setup() { //pin mode OUTPUT data or energy out pinMode(9, OUTPUT); //usually you don't need to set analog pinmode because they always take data in } void loop() { //get Inputs //lower case a0, d1 is not defined. d0 is not defined. valA1 = analogRead(A1); //interact with world //scale the values for the proper OUTPUT //do not have to scale digital INPUT D0 int mapA1 = map(valA1, 0, 1023, 0, 255); //send Outputs analogWrite(9, mapA1); }
Step 8: Taking Your Design to the Next Level
We've shown you how to put together and program a basic digital and analogue circuit with an Arduino.
Now, it's time to play around with different configurations in order to create more complex and fun circuits.
You can have 3 parallel circuits working simultaneously with an input and output at each pin set.
Or use the Fork module to have one input affect up to three outputs!
Step 9: Debugger Starter Sketch
There are many ways you can create a convenient starter sketch for yourself. However, there are time when you need to find out what is going on inside your Arduino. For this situation we have used the starter sketch and added the ability for Arduino to tell you what it is doing via the serial monitor. Feel free to explore and make your own modifications. Compare and contrast the two starter sketches we have provided.
Debugger Starter Sketch:
//declare Inputs //lower case a0, d1 is not defined. d0 is not defined. int valA0; int valA1; int valD0; void setup() { Serial.begin(9600); // we need to send data to our terminal to see what is happening //pin mode OUTPUT data or energy out pinMode(1, OUTPUT); pinMode(5, OUTPUT); pinMode(9, OUTPUT); //pin mode INPUT data or energy in pinMode(0, INPUT); //usually you don't need to set analog pinmode because they always take data in } void loop() { //get Inputs //lower case a0, d1 is not defined. d0 is not defined. valA0 = analogRead(A0); valA1 = analogRead(A1); valD0 = digitalRead(0); //interact with world //scale the values for the proper OUTPUT //do not have to scale digital INPUT D0 int mapA0 = map(valA0, 0, 1023, 0, 255); int mapA1 = map(valA1, 0, 1023, 0, 255); //send Outputs digitalWrite(1, valD0); analogWrite(5, mapA0); analogWrite(9, mapA1); //OUTPUT: debug with serial data Serial.print("valA0: "); Serial.print(valA0); Serial.print(", mapA0: "); Serial.print(mapA0); Serial.print(", valA1: "); Serial.print(valA1); Serial.print(", mapA1: "); Serial.print(mapA1); Serial.print(", valD0: "); Serial.println(valD0); delay(15); }
Step 10: Additional Projects
Now you have learned the rudimentary circuitry and programming involved. Spread your wings and try your own projects or see our other Arduino littleBits Instructables.
Cookie Clicker Build