Showing posts with label Shield. Show all posts
Showing posts with label Shield. Show all posts

19 October 2014

Joystick shields for Arduino

I was playing around lately with some standard I/O boards and came across the joystick shields. There are at least 2 versions of joystick shields on the market. There is one from sparkfun (see link below) and another labeled with funduino that is available in many stores and as well on eBay.

Both share a kind of Nintendo layout using an analog joystick on the left, 4 buttons on the right and 2 buttons in between. The JoyStick Shield V1.a is the one I like to write bout here but I expect that the sources I share with you can help out on other layouts too.

JoyStickShield

The Funduino version has more functionality by providing  switch for the reference voltage of the analog joystick and a lot of connectors especially for the NRF24L01 transceiver. However samples for the shield are hard to find and most stores don’t provide a schema. So here is my documentation that may help you out of this and some sketches to show the functionality.

The JoyStick IO definitions I use are for the Funduino JoyStick Shield V1.A but they can easily be adapted to the sparkfun and other similar joystick shields.

Raw Signal testing

The JoyStick_RawTest.ino sketch that you can find in the library shows how to initialize all the input ports of the Arduino corresponding to the buttons and the joystick of the shield.

Here is the list of the Arduino ports used by the shield buttons and joystick:

function io port
Button A digital pin 2
Button B digital pin 3
Button C digital pin 4
Button D digital pin 5
Button E digital pin 6
Button F digital pin 7
X - Axis analog pin A0
Y – Axis analog pin A1
Joystick push, Button K digital pin 8

In the loop function the real raw signal levels of the IO are printed out to the Serial port so you can inspect the current values. After compiling and uploading the sketch you should open the serial monitor with the 57600 baud settings and watch all the information coming from the shield.
You can see the difference for the 3V3 / 5V switch in the possible values of the analog input signals.

You should use this sketch just to be sure all the IO definitions are fine.

Signal testing

However, using the raw signals is a kind of pain. 2 simple methods can help you to make this more easy. The JoyStick_Test.ino sketch that you can find in the download files shows how to use the input on a more logical level and in respect of the not so exact analog joystick signals.

Buttons

The levels of the buttons are on the level 1 when not pressed. The conversion is implemented in the function joyButton(...) and returns a boolean value.

boolean joyButton(byte pin) {
  int signal = digitalRead(pin);
  return (signal == 0);
} // joyButton

JoyStick position

The levels of the X and Y joystick normally are not very correct. If you leave the stick untouched in the middle position the actual value from the analog port will not be exactly half the reference voltage and it may jump between some values. It is also very hard to get the stick to a specific position and hold it in place. Therefore I use a function that converts to the analog input value to a range of -4 to 4 where 0 is the level in normal position. The conversion is implemented in the function joyStick(...) and returns a signed int. The 3v3 / 5v switch should be in the 5v position.

int joyStick(byte pin) {
  // read the analog value
  int signal = analogRead(pin);
  // convert with mathematics:
  signal = ((signal+64) / 128) - 4;
  return (signal);
} // joyStick

After compiling and uploading the sketch you should open the serial monitor with the 57600 baud settings and watch all the information coming from the shield.

If you like to use these functions just copy them over to your sketch.

...

Sketch JoyStick_RawTest.ino

/// \file JoyStick_rawtest.ino
/// \brief A simple sketch showing the binary input values fro the joystick board.
///
/// \author Matthias Hertel, http://www.mathertel.de
/// \copyright Copyright (c) 2014 by Matthias Hertel.\n
/// This work is licensed under a BSD style license.\n
/// See http://www.mathertel.de/License.aspx
///
/// \details
/// This sketch shows how to initialize all the input ports of the Arduino
/// corresponding to the buttons and the joystick of the shield.
/// The real raw singal levels of the IO are printed out to the Serial port
/// so you can inspect the current values.
/// you can see the difference for the 3V3 / 5V switch in the possible values
/// of the analog input signals.
/// The JoyStick IO definitions  here are for the Funduino JoyStick Shield V1.A.
/// It can easily be adapted to the sparkfun JoyStick Shield.
///
/// More documentation and source code is available at http://www.mathertel.de/Arduino
///
/// History:
/// --------
/// * 18.10.2014 creation.

/// Port definitions for the buttons on the shield with the printed labels
#define BUTTON_A  2 
#define BUTTON_B  3
#define BUTTON_C  4
#define BUTTON_D  5
#define BUTTON_E  8
#define BUTTON_F  7

/// Port definitions for the JoyStick functions
#define ANALOG_PUSH 8
#define ANALOG_X A0 
#define ANALOG_Y A1 


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

  // Setup button inputs with internal pull up high resistor
  pinMode(BUTTON_A, INPUT_PULLUP);
  pinMode(BUTTON_B, INPUT_PULLUP);
  pinMode(BUTTON_C, INPUT_PULLUP);
  pinMode(BUTTON_D, INPUT_PULLUP);
  
  // Setup the option buttons
  pinMode(BUTTON_E, INPUT_PULLUP);
  pinMode(BUTTON_F, INPUT_PULLUP);
  
  // Setup joystick button
  pinMode(ANALOG_PUSH, INPUT_PULLUP);
  // The analog inputs need no setup.
}

void loop() {
  Serial.print(" A:"); Serial.print(digitalRead(BUTTON_A));
  Serial.print(" B:"); Serial.print(digitalRead(BUTTON_B));
  Serial.print(" C:"); Serial.print(digitalRead(BUTTON_C));
  Serial.print(" D:"); Serial.print(digitalRead(BUTTON_D));

  Serial.print(" E:"); Serial.print(digitalRead(BUTTON_E));
  Serial.print(" F:"); Serial.print(digitalRead(BUTTON_F));
  
  Serial.print(" -- X:"); Serial.print(analogRead(ANALOG_X));
  Serial.print(" y:"); Serial.print(analogRead(ANALOG_Y));
  Serial.print(" K:"); Serial.print(digitalRead(ANALOG_PUSH));

  Serial.println();
  delay(250);
}

 

Sketch JoyStick_Test.ino

///
/// \file JoyStick_Test.ino
/// \brief A simple sketch showing the binary input values from the joystick board.
///
/// \author Matthias Hertel, http://www.mathertel.de
/// \copyright Copyright (c) 2014 by Matthias Hertel.\n
/// This work is licensed under a BSD style license.\n
/// See http://www.mathertel.de/License.aspx
///
/// \details
/// This sketch shows how to initialize all the input ports of the Arduino
/// corresponding to the buttons and the joystick of the shield.
/// Here the io signal are converted to be interpreted easily:
/// * The levels of the buttons are on the level 1 when not pressed.
/// The conversion is implemented in the function joyButton(...) and returns a boolean value.
/// * The levels of the X and Y joystick normally are not very correct.
/// They are converted to the range -4 to 4 where 0 is the level in normal position.
/// The conversion is implemented in the function joyStick(...) and returns a signed int.
/// The 3v3 / 5v switch should be in the 5v position.
///
/// The JoyStick IO definitions  here are for the Funduino JoyStick Shield V1.A.
/// It can easily be adapted to the sparkfun JoyStick Shield.
///
/// More documentation and source code is available at http://www.mathertel.de/Arduino
///
/// History:
/// --------
/// * 18.10.2014 creation.

/// Port definitions for the buttons on the shield with the printed labels
#define BUTTON_A  2 
#define BUTTON_B  3
#define BUTTON_C  4
#define BUTTON_D  5
#define BUTTON_E  8
#define BUTTON_F  7

/// Buttons with the names of the classic Nintendo layout
#define BUTTON_SELECT BUTTON_F
#define BUTTON_START BUTTON_E

/// Port definitions for the JoyStick functions
#define ANALOG_PUSH 8
#define ANALOG_X A0 
#define ANALOG_Y A1 

boolean joyButton(byte pin) {
  int signal = digitalRead(pin);
  return (signal == 0);
} // joyButton


int joyStick(byte pin) {
  // 000....128....256....384....512....640....768....896....1024
  // -4     -3     -2     -1      0      1      2      3       4
  int signal = analogRead(pin);
  /// convert with mathematics
  signal = ((signal+64) / 128) - 4;
  return (signal);
} // joyStick


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

  // Setup button inputs with internal pull up high resistor
  pinMode(BUTTON_A, INPUT_PULLUP);
  pinMode(BUTTON_B, INPUT_PULLUP);
  pinMode(BUTTON_C, INPUT_PULLUP);
  pinMode(BUTTON_D, INPUT_PULLUP);
  
  // Setup the option buttons
  pinMode(BUTTON_E, INPUT_PULLUP);
  pinMode(BUTTON_F, INPUT_PULLUP);
  
  // Setup joystick button
  pinMode(ANALOG_PUSH, INPUT_PULLUP);
  // The analog inputs need no setup.
}

void loop() {
  Serial.print(" A:"); Serial.print(joyButton(BUTTON_A));
  Serial.print(" B:"); Serial.print(joyButton(BUTTON_B));
  Serial.print(" C:"); Serial.print(joyButton(BUTTON_C));
  Serial.print(" D:"); Serial.print(joyButton(BUTTON_D));

  Serial.print(" E:"); Serial.print(joyButton(BUTTON_E));
  Serial.print(" F:"); Serial.print(joyButton(BUTTON_F));
  
  Serial.print(" -- X:"); Serial.print(joyStick(ANALOG_X));
  Serial.print(" y:"); Serial.print(joyStick(ANALOG_Y));
  Serial.print(" K:"); Serial.print(joyButton(ANALOG_PUSH));

  Serial.println();
  delay(250);
}

 

Additional Links

http://arduino.cc/en/Main/ArduinoBoardEsplora

http://arduino.cc/en/Reference/EsploraReadJoystickSwitch

03 April 2014

Improved DMX Shield Version for Arduino

The last version of the shield with it’s features and internals can be found on http://www.mathertel.de/Arduino/DMXShield.aspx .

This new version of the shield has no functional additions, so it works exactly like the shield in version v03 that is in use in multiple installations. But there are some improvements by adjusting some values and make its use more flexible when using it on top of an Arduino MEGA and other shields.

The most obvious features of the new (V04) version of the DMX Shield are the better placements of some of the components and the addition of all the shield connectors from the current Arduino boards.

Fixes to resistor values

The values from the optocoupler OK3 transferring the receiving signal in version v03 are adapted to the other optocouplers.

The 3 resistors (R4, R5, R6) are used to limit the current for the internal LEDs of the optocouplers. They had 300 or 470 Ohm in the first layout. These values are now made equal to 470 Ohm which is sufficient.

The 3 resistors (R7, R8, R9) are used to pull the output value of the optocoupler to HIGH also had different values of 470 or 4,7k Ohm. These values are now also made equal to 4,7k Ohm which is also sufficient. The downside of it is that the resistors are hard to distinguish.

I use both adjustments in some installations and they run fine.

The error in the naming of R1 and R2 is fixed.

Here is a picture of the shield with some assembly hints:

DMXShield.v04.hints

Flexibility for Arduino MEGA

The signals RX, TX and D2 are now brought to the pins d0, d1, d2 via a solder jumper. These 3 jumpers should be closed by dropping a small part of solder upon them for normal operation with Arduino Uno or Leonardo Boards.

If you like to use other serial ports or signals like on the Arduino MEGA you can leave these jumpers open and connect the signals the way you like.

The ICSP header of the Arduino board can now be brought to the next shield above the DMX shield by soldering a stackable header.

All the 4 holes from the Arduino Uno Board are now also available to the shield for building permanent robust stacks.

ICSP header and more pins.

All of these extensions are not needed by the DMX shield but it is a good practice to have connection pads and holes at these positions so the signals can be passed through.

Some Arduino shields are using the ICSP header to access the SPI signals from the ATmega chip. The Ethernet shield is a good example for that and there are good reasons to do so.

Not the new version of the DMX shield has 6 contacts right where a ICSP connector can be soldered to pass through the signals to a shield above.

Also 2 of the Arduino header pins have been expanded since some versions. The POWER connector is now 8 pins long and includes IOREF signal. The upper digital connector is now 10 pins long and include the SDA and SCL signals.

Because the DMX shield does not need these signals you can leave these pins without soldering a header or use the (old) shorter headers if you like.

Partlist DMX Shield

Here are the parts you need for a full assembly. It’s also a good order of soldering them.

Part name Value Description
Starting with the low parts...

R4, R5, R6

470 Ω

Bands: (yellow, purple, black, brown, brown*) or (yellow, purple, brown, silver*)

R7, R8, R9

4,7 kΩ

Bands: (yellow, purple, black, black, brown*) or (yellow, purple, red, silver*)

The resistors R1 - R3 should be adjusted for the role of the DMX board in your DMX setup.
This configuration is for a DMX / RDM Controller. Don't use them when implementing a DMX or RDM Slave.

R1, R3

562 Ω

Bands: (green, blue, red, black, brown*)

R2

133 Ω

Bands: (brown, orange, orange, black, brown*)

Now the capacitators...

C1,C2,C3,C4

100n

Capacitors stabilizing the power. Label: 104

The dc-dc converter...

DC1

NME 0505

A dc-dc-converter. The chip that provides another 5 v source for the DMX signal converter.

Now you can check if the secondary 5v power is existing. Put it on top of an Arduino and measure the voltage at the capacitor C4. It should be near by 5V.

The optocouplers and the driver chips...

IC1

MAX481CPA

A MAX481CPA or MAX485 CPA from Maxim or another supplier used to convert the differential DMX signal into a logical signal.
The 8 pin chip can be soldered directly or put on a socket. Be sure to place it in the right direction.

OK1, OK2, OK3

6N137

The optocouplers.
The 8 pin chip can be soldered directly or put on a socket. Be sure to place it in the right direction.

...and the connectors

DMX

 

1X03 pinhead. I cut it from a longer (20 pin) one.

RGB

 

1X04 pinhead

stackable headers

 

You can solder the usual stackable headers or if you use the shield at the uppermost shield on the Arduino you can solder simple pinheads too.

ICSP

 

There is no need caused by this shield to solder a ICSP. You can solder one if you like to use the shield with another shield above that needs these connections.

solder jumpers

 

Don’t forget to close the jumpers when using a standard Arduino.

I will update the article on the web during the next days and will attach some pictures too.

I ordered more than I need for my projects so if you are interested in the PCB – let me know.

24 August 2013

Arduino Shield for NRF24L01+

Here is the picture of a brand new Arduino Shield for the NRF24L01 on top of an Arduino UNO:

NRF24L01Shield.v01

It’s the first version and it works :-) so I finally soldered a transceiver directly onto it.

Signal layout

The signals for the SPI bus come from the ICSP connector to make it compatible to Arduino boards that do not have the classic ATmega328 (2009, UNO) but use other processor ICs like Leonardo, Mega. Here the MIIS, MOSI and SCK signals are not D12,D11 and D9. Using the ICSP connector is compatible.

The CSN and CE signals can be connected to pin D7 and D8 by using little solder drops on top of the shield that you can see on the right. Using these 2 pins because they do not conflict with other cards I use (Ethernet, SDCard). Initialize the RF24 radio class by using:

RF24 radio(7,8);

If other pins must be used, the 2 solder pads can be kept open and some wires can be used to pass the signals.

The IRQ signal is available on pin D3 the same way, but I don’t use it yet.

The rest of the shield has a permanent breadboard layout for small robust implementations.

In the back you can see another connector for I2C bus that I use to connect other hardware like LCD panels.

Prototypes:

Before I had 2 versions in place that I made. The first one was a “flying” transmitter that was perfect for finding the right pins and setup. The second one is sitting on top of an DMX shield.

NRF24L01_Proto1NRF24L01_Proto2

See also:

Hints on using NRF24L01+ with Arduino

This is a first version of a NRF24L01 shield, especially the breadboard it not perfect. I ordered more than I need so if you are working on a NRF24L01 project and like to have one – let me know. (first come first serve)

06 April 2013

Using the SPI bus with Ethernet, SD-card and NRF24L01 and other slave devices together

I currently work on an project using the popular NRF24L01 module where I came across the question how to avoid conflicts of new designs and existing Arduino boards.
The problem when using the W5100 based Ethernet shield including the  SD Card adapter and the NRF24L01 Modules together with a Arduino UNO board is that all of them are using the same SPI bus for the data transfer to the chips. Here is an overview of how to make them work all together.
Here the master is always the same ATMEGA328 chip on the Arduino board and the 3 chips are slave devices.

The SPI bus concept

A brief explanation of the SPI bus can be found on wikipedia. See http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus
The SPI bus needs 4 signals to work:
SCK or SCLK (serial clock) The clock signal that sets the tempo for any data transfer. This signal is generated by the master chip.
On the Arduino Uno this signal is available on pin 13 and ICSP 3.
MISO (master input, slave output) This signal line transfers the data from a slave device to the master chip.
On the Arduino Uno this signal is available on pin 12 and ICSP 1.
MOSI (master output, slave input) This signal line transfers the data from the master to the slave devices.
On the Arduino Uno this signal is available on pin 11 and ICSP 4.
SS or CSN (Slave Select) This signal makes a slave active by selecting it with a low signal.
On the Arduino Uno this signal has to be discussed…
Remark: The signal lines SCK, MOSI and MISO are also available on the ICSP header, see below.
The signal lines SCK, MOSI and MISO can permanently remain connected to all participating chips. However the Slave Select signal has to be different for every device.

Advantages in using the ICSP header

The SPI signals on an Arduino Mega board are not available on the pins 10, 11, 12 and 13 because this board uses another processor. But because programming an ATMEGA chip through the ICSP is internally also using the SPI interface, the signals are available on the ICSP header too. With the Arduino Ethernet Shield Version 06 the design has changed so that the SPI signals are not taken from the pins 11, 12 and 13 but from the pins ISP 4, 1 and 3. If you use an Arduino UNO or 2009 however these pins are connected on the main board.  When designing new shields for Standard and MEGA boards with SPI usage I recommended using the ICSP header like the Ethernet Shield does.

Pin 10

The Arduino pin 10, also labeled SS, also known as PB2 for ATMEGA328, is used by ATMEL to indicate whether the processor is used as an SPI master or slaver device. That pin 10 must remain an output or the SPI hardware will go into 'slave' mode.
Using this pin 10 for example on the Ethernet Shield is fine, but if you use other SPI slave devices, keep in mind to no use this pin for input purpose – better don’t use it to avoid conflicts.

Using multiple SPI bus slave devices

When using multiple slave devices on the same SPI bus there is no principle problem with the lines SCK, MOSI and MISO. Because every slave device needs its own slave select signal there are conflicts using the available Arduino pins. When you stack multiple ARDUINO shields on a single Arduino board you may also have conflicts with other pins the shield uses so you have to take care of other conflicts too. Here are some common pin usages:

Arduino Shield Salve device Arduino Pin
Ethernet version 01 W5100 chip Slave Select 10
Ethernet version 06 W5100 chip Slave Select 10
Ethernet version 06 SD card Slave Select 4

Using a NRF24L01 radio module

In different samples where the NRF24L01 radio is using the Arduino pin 9 for SS and using pin 8 for the Chip Enable signal but also 8 and 7 is used in same samples. There is no standard definition right now and therefore you have to tell the NRF24L01 library what signals to use for Slave Select and Chip Enable.
I personally recommend using 7 for Chip Enable and 8 for Slave Select because the pin 9 is one of the possible PWM output pins. When designing new Shields these pins assignments should be changeable in any way, just to be friendly to other shields… and having the chance to settle conflicts.

More Readings:

Design of the Arduino Ethernet Shield: http://arduino.cc/en/Main/ArduinoEthernetShield

Using the SD-Slot on the EthernetShield with Hardware SPI: http://playground.arduino.cc//Main/EthernetShieldSDHardwareSPIMod General tutorial: http://tronixstuff.wordpress.com/  About the NRF24L01: http://playground.arduino.cc/InterfacingWithHardware/Nrf24L01

22 January 2013

New DMX RDM Library for Arduino available

I just published an new Arduino project on my web site that implements an DMX RDM Device using the DMXShield.

From the beginning the DMXSerial library was designed to send and receive DMX data packets. Now it is extended to support RDM packets as well. I had to overcome several pitfalls and stumbling blocks while extending the DMXSerial implementation and I will keep the current version because it needs only a very small amount of program data. The RDM extended version will be DMXSerial2.

What you can find in the attached zip file right now is an ARDUINO project including all the files you need to compile. Because it is still in WIP (work in progress) I did not extract the DMXSerial2 files into a library format yet.

see: RDM Library for Arduino (DMXSerial2)

10 January 2013

DMX Shield for Arduino is also working with RDM

DMX Shield for Arduino

The last weeks I spent some time in extending the DMXSerial library for Arduino to support the DMX - RDM protocol by using the DMX Shield for Arduino.

A prototype version (proof of concept state) is already working. So now I know that the layout of the shield is RDM compatible.

But there is still some work to do for the library. I’ll try to publish a new version of the DMX library in some weeks.

If you are interested in co-working please let me know.

20 December 2012

New DMX Shields available

Some new DMXShields arrived, tested and running fine.

I already updated the article www.mathertel.de/Arduino/DMXShield.aspx and updated the eagle files in the zip file too even if the text is still related to an older version.

If you have interest in buying one of the PCBs – just let me know.