1 of 23

Arduino 102

Creative Technology Residency

2 of 23

Last Class

  • Arduino hardware overview, sensors
  • Blinking an LED from last class
  • Basics of Arduino programming
  • Discussion of variables, inbuilt Arduino functions, Serial communication
  • Digital vs Analog vs PWM pins
  • Conditional statements
  • Component exploration: buttons, RGB control, potentiometers, joysticks

  • How did the practice exercises go?

3 of 23

Class Outline

  • Functions
  • Loops
  • Addressable LEDs
  • Installing and using libraries
  • Servo motors
  • Learning to use new components independently/building projects: automated plant watering pump
  • Free flex time: explore components and code with guided support, start discussing and designing month 2 build project

4 of 23

Functions

    • A small “chunk” of code
    • Cookbook analogy
    • Functions contained within curly brackets: { }
    • Example of a function: RGB LED
    • Local vs global variables

5 of 23

For Loops

Block of code that repeats instruction over a range of values

Example: fading an LED, accelerating a motor

for (initialization; condition; increment) {

// executable code

}

Example: fading RGB from OFF to red

for(int r = 0; r <= 255; r ++){

writeRGB(r, 0, 0);

}

6 of 23

Nested For Loops

We can put for() loops inside each other

Useful for iterating over multiple varibales/counters

for (initialization; condition; increment) {

// executable code

for (initialization; condition; increment) {

// executable code

}

}

Example: RGB transitions

7 of 23

Smooth Colour Wheel Transitions

A more elegant approach to RGB transitions is the use of a colour wheel

Need to fade in on one colour while fading out on another

8 of 23

While Loops

A loop that executes continuously until the conditional state turns FALSE

Danger of getting stuck in an infinite loop

for() loop

while() loop

for(int r = 0; r <= 255; r ++){

writeRGB(r, 0, 0);

}

int r = 0;

while(r <= 255){

writeRGB(r, 0, 0);

r ++;

}

9 of 23

LED Problems of Scale

10 of 23

Solution? NeoPixels!

  • Addressable LEDs
  • Only need 3 wires to control hundreds or even thousands of LEDs!
  • NeoPixels” is technically a brand name. “WS2812B” is the true LED name.’
  • Come in densities of 30, 60, and 144 LEDs per meter

11 of 23

How to Wire NeoPixels

12 of 23

Installing Libraries & Programming NeoPixels

  • Sketch > Include Library > Manage Libraries > Search “Adafruit NeoPixel
  • Install “Adafruit NeoPixel
  • File > Examples > Adafruit NeoPixel > strandtest
  • Update variables “LED_pin” and “LED_count” (line 20 and 23, respectively)
  • Upload program

13 of 23

Soldering LED strips

  • Pre tin wires and LED pads

14 of 23

What are libraries

Provide extended features specific to individual modules beyond what the standard Arduino environment provides.

An example of a pre-installed library is the Servo library

What is a servo?

15 of 23

A Simpler NeoPixel Program

  • Turn a single LED to one colour
  • Programming languages start indexes at “0”

strip.setPixelColor(index, R, G, B);

Example code:

strip.setPixelColor(4, 255, 0, 0);

strip.setPixelColor(7, 255, 0, 255);

index 4

index 7

16 of 23

Another NeoPixel Program

  • Turn all LEDs to one colour
  • Programming languages start indexes at “0”

strip.fill(colour, first_index, led_count);

Example code:

strip.fill(red, 3, 6);

strip.fill(green, 9, 3);

index 0

index 3

turn 6 LEDs red, then stop

turn 3 LEDs green, then stop

index 9

17 of 23

Alternative NeoPixel Form Factors

  • Rings
  • Flexible Arrays
  • Wearables
  • Flexible Neon Strips
  • Fairy Lights

18 of 23

What is a Servo?

  • DC Motor
  • Potentiometer
  • Constrained rotation [0, 180] degrees
  • Investigate Servo library, open example files

19 of 23

map() function

map() function is used for rescaling a range of values

  • map(value, fromLow, fromHigh, toLow, toHigh)

Exercise:

  • Measure input from a potentiometer, rescale measurement using map() function to a servo angle.
  • Hint: range of potentiometer readings is 0 -> 1023

20 of 23

Exercise: Create a project from the ground up

When starting a new project you may need to use a component you’ve never seen before? How do you learn how to use it?

  • Look for guides/tutorials/documentation online, ask ChatGPT
  • How is it wired to the Arduino?
  • What does the software look like? Does it require additional software libraries?
  • Can you make a unique modification to the tutorial code?

Building an automated plant watering system:

  • What are we looking to achieve?
  • What sensors, actuators, and displays are required for this?
  • Break project down into subsystems and validate individually before integration

21 of 23

Materials

  • DC Motor
  • Motor Driver (product link)
  • OLED Display
  • Protoboard/Breadboard
  • Arduino Nano
  • Jumper wires
  • 5V power supply
  • Barrel jack adapter

22 of 23

Open Discussion: Planning Month 2 Project(s)

  • Personal ideas/designs
  • Looking online for inspiration and project build guides
  • What materials are required
  • Overall design process and steps
  • Making an timeline

23 of 23

Exercises to Practice

  • Independent fabrication of water pump system
  • Sunday support hours
  • Research and plan project builds for next month