Scratch for Arduino
Programming, Electronics, and Giant Killer Robots*
* Giant Killer Robots may be omitted due to budget constraints.
Slide 1
Before we start...
●
We believe in open access to knowledge
●
All our slides are shared online for free
●
You can print it, share it, modify it, use it to run
your own courses
●
This current set of slides can be found here
(* You can also find the URL on your hand-out)
Slide 2
Slides available at: http://a9i.sg/huayi
About Us
YONI
Spent 15 years developing
software for big banks, now
developing the next generation of
Makers and Coders.
CORT
Ex-Navy engineer managing big
engines, powerful generators, and
easily choked toilets. Codes and builds
stuff because he’s too cheap to buy
Slide 3
Slides available at: http://a9i.sg/huayi
What is an Arduino?
Short Answer:
This is an Arduino...
Slide 4
What is an Arduino?
●
First we’ll need to know what is a “micro-
controller”
ATtiny 85 ATmega 328
Slide 5
Slides available at: http://a9i.sg/huayi
What is an Arduino?
Micro-controllers are...
Like a miniature computers...
●
Contains processor, RAM, storage, and more
●
Can be programmed like a computer
Same same...
...but different
Unlike a computer...
●
Provides direct electrical connection to external devices
●
Can’t run Minecraft or Fortnite...
Slide 6
Slides available at: http://a9i.sg/huayi
What is an Arduino?
Micro-controllers can be a pain…
●
Require a special device to program...
AVRISP (In-System Programmer)
Slide 7
Slides available at: http://a9i.sg/huayi
What is an Arduino?
Micro-controllers can be a pain…
●
Require external parts to work (eg. voltage
regulator, crystal oscillator, decoupling
capacitors)
Voltage Regulator Crystal Oscillator Capacitor
(...this is the
simple type)
Slide 8
Slides available at: http://a9i.sg/huayi
What is an Arduino?
So what is an Arduino?
●
Combines a micro-controller with all the other
components into a single device
...and many more
useful
components
Slide 9
Slides available at: http://a9i.sg/huayi
What is S4A?
This is an easy one. It’s a version
of Scratch that works with Arduino.
All of you have already done
Scratch right?
Slide 10
What is S4A?
●
Opensource modification of MIT’s Scratch
●
Modified to communicate with Arduino boards
Slide 11
Slides available at: http://a9i.sg/huayi
What is S4A?
Why use S4A?
●
Code is very similar to Arduino
●
Block-based, so less likely to make mistakes
Slide 12
Slides available at: http://a9i.sg/huayi
What is S4A?
●
Code runs in the computer and sends
commands to the Arduino
Commands
(eg. digital write)
Readings
Runs on Computer (eg. digital read)
Slide 13
Slides available at: http://a9i.sg/huayi
Getting Started
(The fun part...)
Slide 14
Physical Connection
Some of the LED
on the Arduino
should light up
after connection
This end to This end to
computer Arduino
Slide 15
Slides available at: http://a9i.sg/huayi
Software Connection
1) Look for this icon and run it
2) You should have a screen like this...
Slide 16
Slides available at: http://a9i.sg/huayi
Software Connection
3) Wait for it to connect to your Arduino
Once
connected,
it will no
longer show
“Searching”
Slide 17
Slides available at: http://a9i.sg/huayi
First Program
These are key press “events” (found under “Control”).
They run the script beneath them when their keys are pressed.
(If greyed out, check that you’re connected in “Live” mode)
These turn digital pin 13 on and off.
Digital pin 13 is connected to a built-in LED, so you
can see its effect without having to connect anything.
Test it out!
Slide 18
Slides available at: http://a9i.sg/huayi
Exercise 1a
External LEDs
Slide 19
Breadboard
Holes in the
●
Helps to make electrical same row are
connected
connections
No connection
●
Many components (eg. across center
gap
LEDs) can be plugged Holes by the side
are connected
in directly vertically across
entire board
●
Use dupont wires to
make connections Dupont wires
The colors doesn’t
matter; they all work
the same
Slide 20
Slides available at: http://a9i.sg/huayi
LED
●
Light Emitting Diode
●
Longer leg connects to positive
●
Shorter leg connects to ground
(0V)
Short leg Long leg
●
Doesn’t work if connected in negative positive
reverse
Slide 21
Slides available at: http://a9i.sg/huayi
Connecting an LED
Important!!!
Take note of which
leg of the LED is
longer
In this example, the
bottom leg is
longer.
Long leg : Positive
Short leg : Gnd
Slide 22
Slides available at: http://a9i.sg/huayi
Connecting an LED
Connect a wire
from “GND” to the
shorter LED leg.
* My shorter leg is
on top.
Slide 23
Slides available at: http://a9i.sg/huayi
Connecting an LED
Add a 330 ohm
resistor.
Connect one end
to the long LED
leg, and the other
end to an empty
row.
Slide 24
Slides available at: http://a9i.sg/huayi
Connecting an LED
Connect the other
end of the resistor
to the “5V” pin
The LED should
light up
immediately!
5V
Slide 25
Slides available at: http://a9i.sg/huayi
Connecting an LED
This is how the
electricity is
flowing
From…
●
5V to…
●
Resistor to…
●
LED to…
●
GND
5V
Slide 26
Slides available at: http://a9i.sg/huayi
Connecting an LED
Controlling the
LED
To control the
LED, we need to
connect it to an
output pin instead
of 5V.
●
Disconnect it
from 5V.
5V
●
Connect it to
Pin 11.
Slide 27
Slides available at: http://a9i.sg/huayi
Challenges
1) Modify your earlier program to control the
external LED instead of the internal LED.
2) Program the connected LED blink continuously
Slide 28
Slides available at: http://a9i.sg/huayi
Why the resistor?
●
Arduino pins provide 5V
●
Different color LEDs require different voltages
– Red, Yellow, Infra-red: 1.8V
– Blue, White, UV: 3.3V
– Green: Depends. Try 1.8V first.
●
Resistor helps to reduce the voltage
●
What happens when you connect 5V to a Red
LED without a resistor?
Slide 29
Slides available at: http://a9i.sg/huayi
Exercise 1b
External Inputs
Slide 30
Push Button Switch
External View Internal View
Slide 31
Slides available at: http://a9i.sg/huayi
Connecting a Switch
Make these connections
Resistor
Value isn’t as important as
before. Recommend to
pick 1000 ohm, but most
other values will do as
well.
Push button switch
Connects the adjacent
pins. Only need to use
one side...
Connected to 5V
Slide 32
Slides available at: http://a9i.sg/huayi
Connecting a Switch
●
When switch is open…
●
Pin 2 is connected
to 0V via the resistor 5V
●
Pin 2 not connected
to 5V To Arduino
Pin 2
What is the
Voltage of pin 2 voltage?
=0V
= False
= Low
Slide 33
Slides available at: http://a9i.sg/huayi
Connecting a Switch
●
When switch is closed…
●
Pin 2 is connected
to 0V via the resistor 5V
●
Pin 2 is connected
to 5V directly To Arduino
Pin 2
What is the
Voltage of pin 2 voltage?
=5V
= True
= High
Slide 34
Slides available at: http://a9i.sg/huayi
Connecting a Switch
●
This resistor is call a pull-down resistor,
because it pulls the voltage down to 0V when
the switch is open
●
We can also connect the resistor to 5V and the
switch to Gnd. This is call a pull-up resistor.
Slide 35
Slides available at: http://a9i.sg/huayi
Connecting a Switch
●
Can we connect the switch to Gnd without a
resistor?
– NO! Without the resistor, When
we will have a short switch is
5V pressed
circuit when the switch
is pressed
Positive connected
directly to negative.
Short circuit!
Slide 36
Slides available at: http://a9i.sg/huayi
Connecting a Switch
Add this script to your “Device”
Add this script to your “Sprite”
Connected to 5V
Slide 37
Slides available at: http://a9i.sg/huayi
Challenges
1) Make an LED blink rapidly for 2 seconds when
the button is pressed
2) Make an LED toggle between on and off when
the button is pressed
Slide 38
Slides available at: http://a9i.sg/huayi
Copyright
●
Created by A Posteriori LLP
●
Visit http://aposteriori.com.sg/ for more
tips and tutorials
●
This work is licensed under a Creative
Commons Attribution-ShareAlike 4.0
International License.
Slide 39
Slides available at: http://a9i.sg/huayi