Zeven Rodriguez

ITP Fellowship

Arduino Motor Shield

ITP Fellowship, Tutorials

Comments Closed


Share this post

I have been working with the Arduino Motor Shield. Here are some of the basic specs for the shield:

  • Operating Voltage – 5v to 12v
  • Motor Controller – L298P, Drives 2 DC Motors or 1 Stepper motor
  • Max Current – 2A per channel or 4A max (with external power supply)
  • Current Sensing – 1.65V/A
  • Free Running stop and brake function

 

This Shield has 4 main functions:

Function pins per Ch. A pins per Ch. B
Direction D12 D13
PWM D3 D11
Brake D9 D8
Current Sensing A0 A1

The important thing to understand about this shield is the interface. Each channel has independent Direction, PWM, Brake, and Current Sensing. Setting the Direction Digital HIGH or LOW, moves the motor Clockwise or Counterclockwise. The PWM Pin can be set to Digital HIGH or LOW, which turns the motor On or Off, or can we sent and analogWrite to control the speed. When Brake is set to HIGH it forces the motor to Stop.

 

The last feature on the board is the Current Sensing, which you can access by doing a analogRead on the analog 0 and 1 pins. One important thing on the shield is that there are 2 soldered pins on the back that enable this feature.

IMPORTANT: When the current sense pins are soldered they cannot be used as general analog pins. You will get bad readings if you try to use A0 and A1.

 

 

 

 

 

DC Motor

This shield can run 2 DC motors

This is an example of running 2 DC Motors with Current Sense.

 https://github.com/zevenwolf/Motor-Shield/blob/master/DCMotor/DCMotor.ino

Read more

Intro to TSL2561

ITP Fellowship, Tutorials

Comments Closed


Share this post

The TSL2561 is a full spectrum light sensor. I uses 3 light sensors to achieve this. I am using Lady Ada’s guide and library. In Lady Ada’s example, she uses a regular arduino, where the I2C Data and CLk are  pins are pins 4 and 5. I was using a mega which its Data and CLK are pins 20 and 21. In Arduino, the I2C pins are preset. In the example, ladyada prints out the IR, full spectrum, visible, and lux. The sensor maxes out at around 4935. These readings were taken indoors in a room

tsl2561output

 

This is the code used to generate the readings above.

#include <Wire.h>
#include "TSL2561.h"

// Example for demonstrating the TSL2561 library - public domain!

// connect SCL to analog 5
// connect SDA to analog 4
// connect VDD to 3.3V DC
// connect GROUND to common ground
// ADDR can be connected to ground, or vdd or left floating to change the i2c address

// The address will be different depending on whether you let
// the ADDR pin float (addr 0x39), or tie it to ground or vcc. In those cases
// use TSL2561_ADDR_LOW (0x29) or TSL2561_ADDR_HIGH (0x49) respectively
TSL2561 tsl(TSL2561_ADDR_FLOAT);

void setup(void) {
Serial.begin(9600);

if (tsl.begin()) {
Serial.println("Found sensor");
} else {
Serial.println("No sensor?");
while (1);
}

// You can change the gain on the fly, to adapt to brighter/dimmer light situations
//tsl.setGain(TSL2561_GAIN_0X); // set no gain (for bright situtations)
tsl.setGain(TSL2561_GAIN_16X); // set 16x gain (for dim situations)

// Changing the integration time gives you a longer time over which to sense light
// longer timelines are slower, but are good in very low light situtations!
tsl.setTiming(TSL2561_INTEGRATIONTIME_13MS); // shortest integration time (bright light)
//tsl.setTiming(TSL2561_INTEGRATIONTIME_101MS); // medium integration time (medium light)
//tsl.setTiming(TSL2561_INTEGRATIONTIME_402MS); // longest integration time (dim light)

// Now we're ready to get readings!
}

void loop(void) {
// Simple data read example. Just read the infrared, fullspecrtrum diode
// or 'visible' (difference between the two) channels.
// This can take 13-402 milliseconds! Uncomment whichever of the following you want to read
// uint16_t x = tsl.getLuminosity(TSL2561_VISIBLE);
//uint16_t x = tsl.getLuminosity(TSL2561_FULLSPECTRUM);
//uint16_t x = tsl.getLuminosity(TSL2561_INFRARED);
//Serial.println(x, DEC);

// More advanced data read example. Read 32 bits with top 16 bits IR, bottom 16 bits full spectrum
// That way you can do whatever math and comparions you want!
uint32_t lum = tsl.getFullLuminosity();
uint16_t ir, full;
ir = lum >> 16;
full = lum & 0xFFFF;
Serial.print("IR: "); Serial.print(ir); Serial.print("\t\t");
Serial.print("Full: "); Serial.print(full); Serial.print("\t");
Serial.print("Visible: "); Serial.print(full - ir); Serial.print("\t");
Serial.print("Lux: "); Serial.println(tsl.calculateLux(full, ir));
Serial.print("\n");
delay(100);
}

 

 

In this snippet of code, she uses the concept of Bit Math. For more information on this portion of the code read <a title="BitMath" href="http://www.arduino.cc/playground/Code/BitMath">http://www.arduino.cc/playground/Code/BitMath</a>

ir = lum &gt;&gt; 16;

full = lum &amp; 0xFFFF;

&nbsp;

For a simpler example where you just want access to a certain part of the sensor you would use the getLuminosity() function and pass TSL2561_VISIBLE, TSL2561_FULLSPECTRUM, TSL2561_INFRARED into it.

&nbsp;

#include &lt;Wire.h&gt;
#include "TSL2561.h"

// Example for demonstrating the TSL2561 library - public domain!

// connect SCL to analog 5
// connect SDA to analog 4
// connect VDD to 3.3V DC
// connect GROUND to common ground
// ADDR can be connected to ground, or vdd or left floating to change the i2c address

// The address will be different depending on whether you let
// the ADDR pin float (addr 0x39), or tie it to ground or vcc. In those cases
// use TSL2561_ADDR_LOW (0x29) or TSL2561_ADDR_HIGH (0x49) respectively
TSL2561 tsl(TSL2561_ADDR_FLOAT);

void setup(void) {
Serial.begin(9600);

if (tsl.begin()) {
Serial.println("Found sensor");
} else {
Serial.println("No sensor?");
while (1);
}

// You can change the gain on the fly, to adapt to brighter/dimmer light situations
//tsl.setGain(TSL2561_GAIN_0X); // set no gain (for bright situtations)
tsl.setGain(TSL2561_GAIN_16X); // set 16x gain (for dim situations)

// Changing the integration time gives you a longer time over which to sense light
// longer timelines are slower, but are good in very low light situtations!
tsl.setTiming(TSL2561_INTEGRATIONTIME_13MS); // shortest integration time (bright light)
//tsl.setTiming(TSL2561_INTEGRATIONTIME_101MS); // medium integration time (medium light)
//tsl.setTiming(TSL2561_INTEGRATIONTIME_402MS); // longest integration time (dim light)

// Now we're ready to get readings!
}

void loop(void) {
// Simple data read example. Just read the infrared, fullspecrtrum diode
// or 'visible' (difference between the two) channels.
// This can take 13-402 milliseconds! Uncomment whichever of the following you want to read
// uint16_t x = tsl.getLuminosity(TSL2561_VISIBLE);
uint16_t x = tsl.getLuminosity(TSL2561_FULLSPECTRUM);
//uint16_t x = tsl.getLuminosity(TSL2561_INFRARED);
Serial.println(x, DEC);
delay(100);
}

Read more

Working with Rotary Encoders

ITP Fellowship, Tutorials

Comments Closed


Share this post

A rotary encoder is a digital rotary sensor. It has the capability of rotating 360 degrees and can tell you direction. This tutorial is based on Paul Stoffregen’s Encoder library and explanations.

Rotary Encoder

encoder

Inside of a rotary encoder are 2 contacts that are read by the arduino and the center contact grounds the encoder. The rotary encoder has a built in pattern that helps determine the direction you are spinning. The rotary encoder is essentially 2 switches. When Pin 1 goes high you are moving left. When Pin 2 goes high you are going right.

Rotary Encoder

Pin 1 == Low & Pin 2 == Low ==> Position 1

Rotary Encoder

Pin 1 == High & Pin 2 == Low ==> Position 2.  We know that we have moved left.

The arduino pins read the  HIGH and LOW of the pins to determine the direction. To simplify this process we are using this library http://www.pjrc.com/teensy/td_libs_Encoder.html. I used the basic code in the library example.

Arduino

/* Encoder Library - Basic Example</div>
* http://www.pjrc.com/teensy/td_libs_Encoder.html</div>
This example code is in the public domain.
*/

#include &lt;Encoder.h&gt;
// Change these two numbers to the pins connected to your encoder.
//   Best Performance: both pins have interrupt capability
//   Good Performance: only the first pin has interrupt capability
//   Low Performance:  neither pin has interrupt capability
Encoder myEnc(5, 6);
//   avoid using pins with LEDs attached
void setup() {
Serial.begin(9600);
Serial.println("Basic Encoder Test:");
}
long oldPosition  = -999;
void loop() {
long newPosition = myEnc.read();
if (newPosition != oldPosition) {
oldPosition = newPosition;
Serial.println(newPosition);
}
}

As you can see above Pin 5 and Pin 6 are the pins the arduino is going to read in order to determine which pin is high or which pin is low.


encoder serial monitor

Advanced Topic: Inverse Logic and Arduino Pull-up

The Rotary Encoder is essentially 2 switches. This library sets both pins as INPUTS, followed by setting a digitalWrite to HIGH. This engages the arduino’s pull-up resistor for that pin. What the pull-up resistor does it allows a small trickle of positive voltage into the pin. Setting that pin to constantly read HIGH until you rotate the knob where the state would change to LOW. We normally think in HIGH being On and LOW being Off. With Inverse Logic HIGH is your Off state and LOW is your On state. In the case of the rotary encoder, when the pin makes contact with the white area, the encoder makes a connection to ground. Ground is now the path of lease resistance. So no electricity is flowing through the pull-up resistor, thus the pin goes Low. The orange represents the path of least resistance and the flow of electricity.

pullupresistor

Note: This library can use the arduino interrupt pins for better performance.

Read more

ChronoDot and Arduino 1.0

ITP Fellowship, Tutorials

Comments Closed


Share this post

Recently I updated LadyAda’s RTC Library for Arduino 1.0 and older versions. In arduino 1.0 the wire library changed. It is now wire.read and wire.write. I also found that when you do use the write(0), Arduino would interpret the 0 as an integer. The new wire library does not. Because of this I had to explicitly say int i = 0;

The code can be found here https://github.com/adafruit/RTClib

ChronoDot and Arduino

The red wire power goes to 3.3v. Black goes to ground. SDA (Data), the green wire, goes to Analog Pin 4. SCL (Clock), the yellow wire, goes to Analog Pin 5.

Read more

Sparkfun OpAmp Breakout LMV358

ITP Fellowship, Tutorials

Comments Closed


Share this post

The LMV358/LMV324 are low voltage (2.7–5.5V) versions of the dual and quad op amps, LM358/LMV324, which currently operate at 5–30V. If you need to use a piezo as an analog sensor into the arduino, this is the chip and breakout board to use. Most OpAmp chips operate by looking at 2 voltages and amplifying the difference between them.   The chip has a pot that lets you dial in the amplitude of that difference. One thing I noticed is that when I hit the piezo you get negative values. This can be fixed by using an absolute value function. This happens because in order to contain huge spikes in the voltage of a piezo(+30v), the breakout board uses a voltage divider which manages that voltage and sets the output of the OpAmp at 2.5 volts. Tom and I used an oscilloscope to verify this. This is important because the when you hit the piezo it spikes to +5 and to 0. This makes sure that we do not send too much positive voltage to the arduino and negative voltage which would fry it.

http://www.sparkfun.com/products/9816

Sparkfun OpAmp Breakout LMV358

Read more