RobotC-I2C Interface Packet I Storming Robots Computational Thinking and Engineering i ROBOTC-I2C AND ARDUINO INTERFACE PACKET Before you use this document: Unless otherwise noted, Storming Robots retains an “All Rights Reserved” copyright, pursuant from the day this document was published by Storming Robots. This means that you are NOT allowed to copy them and use them on your own site or other publication without permission. This is SOLELY used for you to view, but NOT for redistribution for any purpose. Scope: This tutorial will guide to program NXT as a master I2C with Arduino/Nano as the I2C slave device. Although this document will cover how to hook up basic analog and digital sensors/devices with the Nano, this is NOT meant be an electronic tutorial. Before you work on this document, you must have completed RobotC I2C Packet I. Note: I2C protocol communication can be very extensive and complex. For example, what if a sensor module is abruptly terminated. The Arduino has a tendency holds the line LOW and will no longer responses with proper data until you manually reset the controller. Or You should perform good amount of error checking in order to make your new sensors module robust to use as well. If you are going to write I2C interface, consult the RobotC- internal I2C APIs regarding the error checkings. - In common.h (don’t ask me why they put functions inside a header file!!!) - I2C functions such as : o writeI2C o waitForI2Cbus o clearI2CError o sendI2CMsg By :Elizabeth Mabrey
21
Embed
ROBOTC-I2C AND ARDUINO INTERFACE PACKET RobotC-Arduino Packet.pdftutorial. Before you work on ... Arduino Nano board pin map: ... computer, or full set of batteries in the power pack.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
RobotC-I2C Interface Packet I Storming Robots
Computational Thinking and Engineering
i
ROBOTC-I2C AND ARDUINO INTERFACE PACKET
Before you use this document:
Unless otherwise noted, Storming Robots retains an “All Rights Reserved” copyright, pursuant from the day this document was
published by Storming Robots. This means that you are NOT allowed to copy them and use them on your own site or other
publication without permission. This is SOLELY used for you to view, but NOT for redistribution for any purpose.
Scope: This tutorial will guide to program NXT as a master I2C with Arduino/Nano as the I2C slave device. Although this
document will cover how to hook up basic analog and digital sensors/devices with the Nano, this is NOT meant be an electronic
tutorial.
Before you work on this document, you must have completed RobotC I2C Packet I.
Note: I2C protocol communication can be very extensive and complex. For example, what if a sensor module is abruptly
terminated. The Arduino has a tendency holds the line LOW and will no longer responses with proper data until you
manually reset the controller.
Or
You should perform good amount of error checking in order to make your new sensors module robust to use as well. If you
are going to write I2C interface, consult the RobotC- internal I2C APIs regarding the error checkings.
- In common.h (don’t ask me why they put functions inside a header file!!!)
This document Scope .............................................................................................................................................................................................. 1
Safety for yourself and your electronic components 1
Ch1 - Some Basics on the Arduino side .................................................................................................................................. 3
ch1 -1) Basics Parts Used in this Packet ............................................................................................................................................................ 3
ch1 -2) Some Basic terms used here................................................................................................................................................................... 6
Ch2 -Connecting sensors with Arduino .................................................................................................................................. 8
ch2 -2) Learn From Samples (Sketch-C IDE) ................................................................................................................................................. 9
Exp 1 - Blink on embedded LED on Pin 13 9
Exp 2 - Blink an external LED 9
Exp 3 - Blink an external LED with a push button 10
Exp 4 - Turn on/off LED with potentiometer 11
Exp 5 : Dim/Brighten LED with potentiometer 11
Exp 6 :Bonus Challenge - Light up 2 LEDs (connected in Parallel) 11
ch2 -3) Connect to the Parallex Ping - the Ping Pulse ............................................................................................................................. 12
Ch3 Talk I2C between NXT and Arduino/Nano ................................................................................................................... 13
ch3 -3) APIs that you need to know.................................................................................................................................................................. 15
Header file and basic APIs for the I2S Slave module .................................................................................................................................. 15
Electricity and Magnetism -. Electricity is the movement of electrical charge from one place to another. Electric charges do not
exist without their associated electric and magnetic fields. All particles are or become electrically charged. The smallest known
charge of electricity is the charge associated with an electron. This charge has been called a "negative" charge.
Electronics deals with development and application of devices which apply electrical current flow of electrical charges to
circuits to accomplish specific tasks.
TERMS IN BASIC ELECTRICITY
Terms Unit of Measurement for it
Current (I stands for Intensity)
The rate of flow of electrons through a component. In other words, is the rate at which electric charge flows past a point in a circuit. It is like the amount of water thru a pipe. Do note current flows from negative end of a power source to positive end. (contradicting to the conventional )
Static electricity : electrical charges are at rest,
Electric current : charges are in motion.
Amperage ( A ) = The unit of measurement for “current”. One ampere means the amount of coulomb of electrical charge passes through a point in one second. Named for André Ampere, a French physicist and mathematician who was one of the founders of the science of classical electromagnetism back in early 1800s.
1 A = 6.24 * 1018 electrons flowing through a certain point per second
Voltage ( V or F stands for Electromotive Force)
The force or electrical pressure that is necessary to move electrical current, i.e. to drive electrons to flow past a point in a circuit.
Volt (V)
To measure the force of electrical pressure, or electrical potential.
It was named after the Italian physicist Alessandro Volt, early 1800s, who invented possibly the first chemical battery.
Resistance (Ohms - R ): The opposition to current flow in a circuit; measured in ohms.
Ohms ()
The law was named after the German physicist Georg Ohm back in 1800s.
is circular path which gets electricity to flow and do something useful.
SHORT Circuit : BAD! This is to connect a wire directly from the positive to the negative side of a power supply,
you’ll create what is called a short circuit. DON’T DO IT. This could cause your wire to burn up, damage the
power supply, drain your battery, or other hazardous things like exploding.
The bottom line: if you notice that things are suddenly becoming hot or a part suddenly burns out,
immediately turn off the power and look for possible short circuits.
OPEN VS. CLOSED CIRCUIT:
Open circuit is a path where the loop isn’t fully connected (i.e. this isn’t really a circuit at all). TIP: if you can’t
easily find where your circuit is open, a multimeter can be very useful tool
Closed circuit is a path which completes the circuit.
DIGITAL VS ANALOG
Analog : 1024 states: values from 0 to 1023
Digital : two states == HIGH/LOW
VIN VS. VOUT
Vin == the power source, e.g. 6V batteries
Vdd == the power supplied to the chip, e.g. onboard 5V / 3.3V pin. (many of our diagrams simply use them
interchangeably as they essentially meant the input. )
Vout == voltage output, usually the feedback value from sensors
HIGH IMPEDANCE
In simplest term - Resistance Current
LOW IMPEDANCE:
In simplest term - Resistance and Current
RobotC-I2C Interface Packet I Storming Robots
Computational Thinking and Engineering
8
CH2 -CONNECTING SENSORS WITH ARDUINO
CH2 -1) ARDUINO NANO BOARD PIN MAP:
Analog Pin A0 to A7 : 10-bits resolution. - With pinMode(), ananlogRead(), analogWrite() - A4 & A5 == SDA & SCL respectively (but may be
different in various versions)
Internal
LED
D2 to D13: Digital pins. (Don’t use D0 and D1 (used for Serial communication). - With pinMode(), digitalRead(), digitalWrite(), analogWrite()
5V – power line
GND - ground
RobotC-I2C Interface Packet I Storming Robots
Computational Thinking and Engineering
9
CH2 -2) LEARN FROM SAMPLES (SKETCH-C IDE)
Best way to learn from Samples is :
- Compile and download to ensure it is working
- Read the code and understand how to use the APIs
- Rewrite your own by switching the pins connection.
EXP 1 - BLINK ON EMBEDDED LED ON PIN 13 Test the “Blink” Sample program from Sketch
Note : Pin 13 has an LED connected on most Arduino boards.
EXP 2 - BLINK AN EXTERNAL LED Connect an external LED to digital pin 3, and write a program to blink it.
Serial monitor
RobotC-I2C Interface Packet I Storming Robots
Computational Thinking and Engineering
10
EXP 3 - BLINK AN EXTERNAL LED WITH A PUSH BUTTON a) Connect and write a program to turn on the LED when pushed, off when not pushed. (Pull-down resistor circuit)
b) Use the internal pull-up resistor circuit, or you can do it yourself.
Note: you need to do: pinMode(9, INPUT_PULLUP);
Review:
Pull up == the input line needs pull up… i.e. high by default ….. i.e input line with the resister connects to power. Pull down == the input line needs pull down … i.e. low by default .. ….. i.e input line with the resister connects to Ground.
RobotC-I2C Interface Packet I Storming Robots
Computational Thinking and Engineering
11
EXP 4 - TURN ON/OFF LED WITH POTENTIOMETER Refer to Examples Analog AnalogInput
EXP 5 : DIM/BRIGHTEN LED WITH POTENTIOMETER
Now, program your code to brighten/dim your LED instead of on or off.
Refer to Examples Analog Fading
EXP 6 :BONUS CHALLENGE - LIGHT UP 2 LEDS (CONNECTED IN PARALLEL) Note that the LEDs are connected in parallel circuit. To each of the LED itself, the circuit to it is not in Parallel. However the
circuit system as a whole is connected in Parallel. This diagram hooks to another controller board. You need to create your own
circuit using NANO instead.
Program: Turn on led on B7 when the analog value of the
potentiometer > 28
Turn on led on B6 when the analog value of the
potentiometer > 27
RobotC-I2C Interface Packet I Storming Robots
Computational Thinking and Engineering
12
CH2 -3) CONNECT TO THE PARALLEX PING - THE PING PULSE
Since this sensor module involves a ping (a very commonly used sensor), we should briefly get an idea how the Ping signal
works. You should review the Ping code from Arduino and fill in below… …
:
0•pinMode to OUTPUT
1 • We make signal line go LOW for _________ microseconds. (delayMicrosecond() )
2• Set Signal Line HIGH for _______ microseconds
3• Here we set signal line LOW. This activates the PING sensor and it sends a sonic pulse
4
•PING sensor will set the signal line HIGH for the amount of time it took for the 'ping' to make its round trip. This is the duration what wemeasure and convert to distance
5• Immediately followed by pulseIN() or micros() function
1 0 5
Hint:
1. We make signal go LOW for 2 microseconds,
then set signal high for 5 microseconds
2. Do not forget to change the pinMode for
the SIG line to INPUT after step 3. That
means we must make sure we set the SIG
line to OUTPUT prior to step 1.
RobotC-I2C Interface Packet I Storming Robots
Computational Thinking and Engineering
13
CH3 TALK I2C BETWEEN NXT AND ARDUINO/NANO
CH3 -1) SAMPLE CONNECTIONS DIAGRAM
Optional : a NXT breadboard adaptor from Mindsensors or Dexter Industries. OR simply strip one end of a NXT wire.
A few 82K and/or 43K resistors. Typically resistor values of 82k are used. If the device you are communicating with is operating at 3.3V (rather than 4.7V) you can use 43k resistors. Resistor values are important to ensuring you can communicate at a fast speed with the NXT.
via NXT breadboard adaptor from Dexter Industries
Or simply strip the NXT wire
via NXT breadboard adaptor from Mindsensors.
+ two 83K resistor
RobotC-I2C Interface Packet I Storming Robots
Computational Thinking and Engineering
14
CH3 -2) SIMPLIFIED HANDSHAKING
Note: This is an over-simplified flow in order to make it easier to understand as introduction.
ONE WAY HANDSHAKING
BI-DIRECTIONAL HANDSHAKING
RobotC-I2C Interface Packet I Storming Robots
Computational Thinking and Engineering
15
CH3 -3) APIS THAT YOU NEED TO KNOW
HEADER FILE AND BASIC APIS FOR THE I2S SLAVE MODULE
#include <Wire.h>
volatile uint8_t Request;
//…some global data. E.g. one of the sensors is ping, and one function request
//from the master is to turn blink on or off
volatile uint8_t PingValue;
volatile bool Blink=false;
void setup()
{
… Wire.begin( );
… Wire.onReceive( recEventFunction );
… Wire.onRequest( reqEventFunction);
}
Void loop()
{
PingValue = …some function to obtain the distance
}
void recEventFunction( int nbytesRead )
{ …
Request = Wire.read(); …
}
void reqEventFunction()
{
Switch (Request)
{ case ….
}
}
List of APIs:
begin()
requestFrom() // used by master device only. Not applicable for our sample
beginTransmission() // used by master device only. Not applicable for our sample
endTransmission() // used by master device only. Not applicable for our sample
write()
available()
read()
onReceive()
onRequest()
Consult www.arduino.cc/en/Reference/Wire for more details.
Caution:
recEventFunction() and reqEventFunction() are called based on something called system interrupt. For the sake of staying within
the scope of this document. We won’t go into that. However, one thing you MUST know is that these routines MUST return AS
Use your Arduino as your master platform. Write a test to send “int” data fields, and sizeof(int) ==2. Sample code segment
const byte slaveAddress = 0x02; // assuming one datatype of int == 2 bytes unsigned long lastSerialPrint = 0; void setup() { Serial.begin(9600);
Wire.begin(); } // end of setup void loop() {
if ( Wire.requestFrom (slaveAddress, sizeof(myData)> 0) myData = Wire.read (); if( millis() - lastSerialPrint > 1000 ) //Like the Blink without delay example, true once a second { Serial.println (myData); lastSerialPrint = millis(); //Snapshot of when this happened, in milli seconds } // end if
} // end of loop
ADDITIONAL CHALLENGE:
What if your master code is sending over 4 “int” data fields?