BasicMicroUK - Forums

www.basicmicro.co.uk
It is currently Tue Jul 17, 2018 8:04 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Mad Hatter Experiments
PostPosted: Wed May 11, 2011 8:03 pm 
Offline
Master

Joined: Tue Jun 22, 2010 9:15 pm
Posts: 228
MAD HATTER EXPERIMENTS
5/9/11
Alan Marconett KM6VV

A good starting place to learn a new board is to perform a few experiments that can be done with a minimum of additional hardware. Since Basic Micro has a new board, the “Mad Hatter” using the 3687 processor with the capability to use Arduino shields (daughter boards) and the Atom Basic, I was interested!

Image


Fig 1 Basic Micro Mad Hatter Board

A few flashing LEDs
The classic (and easy) place to start is to wire up a few LEDs, and get them to flash. Although we could just plug the wire leads of the components (two resistors and two LEDs) into the connector strips of the Mad Hatter board, we soon find out that that we have more wire “ends” then convenient places to put them! The quick solution is to use a simple protoboard. There are several compatible boards (shields) from which to choose. A $6 board (no parts) is available from Mouser. Or get a few parts with it for a few dollars more. Sparkfun has a very nice one with parts as well. We’ll also want the little $6 solderless breadboard that can be attached to it. The other parts needed are some .025” posts and jumpers. I like the ones made by SchmartBoard (available from Mouser).

Image

Fig 2 Chassis Top View


You’ll see that I’ve attached my Mad Hatter board to a chassis with 3/8” long 4-40 hex aluminum spacers, nylon screws, and washers (we don’t want to short out traces on the board). Also, watch the height of the three electrolytic caps, and add electrical tape insulation if needed to prevent them from touching the prototype or other daughter board (shield). I’ve got a few extra holes in my chassis, the use of which will become apparent later. A drawing will be available for download as well. Some will just want to use the board as-is. That is OK too.

Image

Fig 3 Chassis Bottom View

Image

Fig 4 Chassis with Mad Hatter mounted


Start by soldering the two LEDs and two resistors onto the board. Watch the polarity of the LEDs! The short wire goes to the “gnd” side. Also separate and solder in the pin strips for the four stacking connectors. Use the outside rows (check them with the MH board). Many boards include “stacker” pin strips to go on top of the board. The Sparkfun boards work this way. For our use, I’m doing something a little different; I use the regular .025” pins in the holes adjacent to the connectors, this way I can use my pre-made jumpers. One can also pull the pins out of the insulating strips of the break-off pin strips, and press the pins into the stacking connectors or the solderless breadboard. Now the jumpers can be used for all wires. Or you can just use 20 ga solid hook-up wire. Your board should look like mine in the photo. The two pins installed in the LED area are jumpered (red jumpers) over to the P6 and P7 pins on the board’s inboard connectors.


Image

Fig 5 Prototype Shield

Image

Fig 6 Prototype Shield and parts

Image

Fig 7 Prototype Shield stuffed and mounted



Plug in a standard USB cable into the Mad Hatter board. We’ll assume that you already have the Basic Micro Studio installed on your machine. Use version 2.0.0.11 or later and insure that the proper chip is recognized (Mad Hatter, or possibly Arduino). Load and compile MadHatterLED1.BAS, and program the MH board. There is a “Build MadHatterLED1.BAS” button, and a “Program” button on the Basic Micro Studio screen.

You won’t need any external power for these experiments, just the built-in USB. Upon conclusion of the download, you should be rewarded with the LEDs flashing in an alternating pattern!


Image

Fig 8 LED Experiment schematic


In the Program
So what’s in the program? Let’s take a look. I like to start my programs off with a header giving the name, purpose, date and author of the program. Next, although not necessary, is a complete listing of the pins of the board by connector (CN1 – CN3 and Power). I’ve noted on P6 and P7 that there are LEDs attached. Doing this documents at least part of your board’s wiring.

Next you’ll see ENABLEHSERIAL. This does what it says! It enables our hardware serial port along with the SetHSerial1 statement that follows the BAUD and LED defines. Then a hserout statement. What do we need the serial stuff for? OK, not much in this simple program, but you’ll see that we cause the program to print out its name on a terminal. We enable terminal1 and set it to a matching 9600 baud for this. More elaborate programs can print out additional data, or “Hello World”! A pair of output commands follow to set the P6 and P7 pins up for output to our LEDs.

Next we see a “main” marking off the loop of the program. In our loop are statements to set the LEDs high (on) or low (off). The “pause” statements slow up the steps of the program by 500 milliseconds each, so that we can follow the flashes!

Code:
ENABLEHSERIAL

;Defines
H_SER_BAUD   con   H9600      ; for hardware serial port
LED1 con P6
LED2 con P7

SetHSerial1 H_SER_BAUD,H8DATABITS,HNOPARITY,H1STOPBITS
hserout ["Mad Hatter LED 1",10,13]   ; Sign on to terminal

output LED1
output LED2

main

 high LED1   ; LED1 ON
 low  LED2   ; LED2 OFF

pause 500

 low  LED1    ; LED1 OFF
 high LED2   ; LED2 ON

pause 500

goto main


Simple enough? Yes. But it gives us a place to start. Try other pins! Change the speed! Change the message or send new messages to the terminal!

Switching Things Up
For our second experiment we’ll add a switch to the circuit (I had a microswitch handy) and change the program a bit. The switch goes to P5 and ground (I soldered bare pins to the N.O. pins of the switch), use jumpers (yellow this time) to connect the switch. I pulled out the pins from a strip of pins, and pushed them into the breadboard to allow me to use my pre-made jumpers. You’ll also need to add a 1K resistor from a 5V pin on the board to the “high” end (P5) of the switch. If this resistor is not included, the LEDs will flash erratically because the input pin is floating, and not driven high.

Image

Fig 9 Solder-less Breadboard

Image

Fig 10 Switch Experiment schematic

Image

Fig 11 Switch Experiment



Load the new code “MadHatterSwitch2.BAS”. The first program has been modified a little. Instead of delays, we now have a conditional statement, the “if(IN5)”. IN5 does a read of pin 5 (which we connected to our switch) for us. The remainder of the conditional (if-then-else) statement selects the top pair or bottom pair of statements to control the LEDs, depending on the state of the switch. Still a simple program, but we’ve now exercised input and output statements, a print statement (hserout) and a conditional statement. All useful stuff!

Code:
;Variables
P5_data var byte

;Defines
H_SER_BAUD   con   H9600      ; for hardware serial port
LED1 con P6
LED2 con P7

ENABLEHSERIAL
SetHSerial1 H_SER_BAUD,H8DATABITS,HNOPARITY,H1STOPBITS
hserout ["Mad Hatter SWITCH 2",10,13]   ; Sign on to terminal

output LED1
output LED2

main
 
 if(IN5) then      ; read the switch
   high LED1      ; LED1 ON
   low  LED2      ; LED2 OFF
 else
   low  LED1       ; LED1 OFF
   high LED2      ; LED2 ON
 endif

goto main


Image

Fig 12 Switch Experiment close-up



Go Analog!
This time (third experiment) we’ll do a little analog (A/D). Now the solderless breadboard really comes in handy! I had a small 25K pot (multiple turn) with through-hole pins handy, so I used it. A Pot with a shaft would be easier to use then the screwdriver slot on my “trimmer” pot. Feel free to substitute! 5K to 30K (linear) will work for the pot. You’ll probably have to solder wires (solid) onto the leads of a big pot (or use green jumpers cut in half); my little pot just pushes into the breadboard. Connect the high end of the pot to +5V through a 100 or 220 ohm resistor to limit current into the analog input pin. Connect the center “wiper” pin of the pot to A0 (analog input 0) on the board. Connect the cold end of the pot to ground. See schematic.

Image

Fig 13 Pot Experiment

Image

Fig 14 Pot Experiment close-up

Load and compile MadHatterPOT3.BAS. This program has a few new instructions. The adin instruction “reads” the voltage on the A0 pin (mapped to the processor’s P14). Once again we’ve used constants and defined AD_0 for convenient use in the program. We also reserve space for a 16-bit “word” of storage to hold the 10-bit data that we will read from the pot. This program does some calculations to determine our voltage reading and print it out once again, to our handy terminal. This is called scaling. We convert the 0-1023 integer value obtained from the A/D conversion, and “scale” it to match the 5v reference that we’ve supplied to the pot.

Just after the print statement, we see the raw A/D value being compared to the “middle” value of the A/D range. This value (512) represents 2.5V.

So what happens when we run the program? If we open up terminal1 in the Basic Micro Studio and watch the constant stream of “AD 0 voltage:” messages, we’ll see the value change as we adjust the pot. Cross back and forth around the magic value of 2.5V, and you’ll see the pattern of the LEDs toggle back and forth.

Code:
;Defines
AD_0 con P14
LED1 con P6
LED2 con P7

;Variables
AD0_data var word   ; 25K pot 3266W-253-ND

ENABLEHSERIAL
H_SER_BAUD   con   H9600      ; for hardware serial port
SetHSerial1 H_SER_BAUD,H8DATABITS,HNOPARITY,H1STOPBITS

hserout ["Mad Hatter POT 3",10,13]    ; Sign on to terminal

output LED1   ; set pins for output to LEDs
output LED2

main

 adin AD_0, AD0_data     ;read the pot
 hserout [dec A0_data, 13,10]
 hserout ["AD 0 voltage is: ",real (TOFLOAT AD0_data)/1024.0*5.0\2,"v",13,10]
 
 if(AD0_data > 512) then   
   high LED1      ; LED1 ON
   low  LED2      ; LED2 OFF
 else
   low  LED1       ; LED1 OFF
   high LED2      ; LED2 ON
 endif

goto main


So what new have we learned? We’ve read an A/D input. We’ve converted an integer to a floating point value, and we’ve displayed it. All useful stuff.

Rock My Servo!
In this forth example we’ll command a hobby R/C servo. The program builds upon our last experiment with the pot. You were wondering what the cutouts in the side of the chassis were for? Well, they make a nice place to mount an R/C servo! If desired, mount one on your chassis. Mechanically center your servo. I suggest attaching a servo horn with a thin strip of black electrical tape to aid in observing the rotation of the servo. Or you could get fancy and add a compass rose!

Image

Fig 15 Servo Experiment

Image

Fig 16 Servo Experiment close-up



You’ll need to connect up a standard hobby R/C servo. These servos have a three-pin connector housing, made to slip over three of out pins. Some shields (boards) already are configured with convenient pins arranged for this. We’ll just use three bare pins (pulled out of a strip of pins), and insert them into our solderless board to make our connections. We’ll use three adjacent rows of pins (see pix) to make our connections (similar to connecting up a pot). The black wire of the servo goes to ground, the red to +5V, and the yellow (or white) wire goes to P5 (last used for the switch, unplug the switch first). That should get you connected!

Image

Fig 17 Servo mounted

Load and compile MadHatterServo4.BAS. You’ll see a new print command (hserout) to show us the raw data, and another one for showing the results of the ServoPosition calculation. To use R/C servos, we generate a special PWM that outputs a 500uS to 2500 uS pulse width every 20 mS. We send values of about -16000 to 16000 via the hservo command (for my Futaba S3004, anyway).

Code:
;Defines
AD_0 con P14
LED1 con P6
LED2 con P7
RC_Servo con P5

;Variables
AD0_data var word   ; 25K pot 3266W-253-ND
ServoPosition var sword      ;Futaba S3004

ENABLEHSERIAL
;H_SER_BAUD   con   H38400      ; for hardware serial port
H_SER_BAUD   con   H9600      ; for hardware serial port
SetHSerial1 H_SER_BAUD,H8DATABITS,HNOPARITY,H1STOPBITS

hserout ["Mad Hatter SERVO 4",10,13]   ; Sign on to terminal

output RC_SERVO   ; pin for servo
output LED1   ; set pins for output to LEDs
output LED2

ENABLEHSERVO
hservo [RC_SERVO\0] ;default servo to center position

main

 adin AD_0, AD0_data ; read pot voltage
 
 hserout [dec AD0_data, 13,10]
 hserout ["AD 0 voltage is: ",real (TOFLOAT AD0_data)/1024.0*5.0\2,"v",13,10]

 ServoPosition = ((2 * AD0_data) - 1024) * 20     ; scale the servo position
 hservo [RC_SERVO\ServoPosition] ;default servo to center position     ; set the servo position
 
 hserout ["Servo: ", sdec ServoPosition, 13, 10]   ; report the servo positon
 
 if(AD0_data > 512) then   
   high LED1      ; LED1 ON
   low  LED2      ; LED2 OFF
 else
   low  LED1       ; LED1 OFF
   high LED2      ; LED2 ON
 endif

goto main


NOTE: Start out with the pot centered (2.5V), and carefully explore the range of your servo. Servos can be damaged if forced past their normal ranges.

Download the code, and when the program is running, adjust the pot and watch the servo move!

Summery
We’ve learned how to control LCDs, read switches, and position servos. Humm, if I attach a second servo, and mount some wheels… Well these 180 degree servos won’t get you very far with wheels attached. But they DO make continuous rotation servos. You’ve probably guessed that my chassis is intended for a small robot! You’d be correct! A future article will explore a shield to drive a pair of R/C servos. (There are actually two ways to drive the servos, as we’ll see).



BOM
Mouser:
782-A000024 Prototype board
872-920-0008-01 Prototyping Products 3” Red Jumpers
872-920-0009-01 Prototyping Products 5” Yellow Jumpers
872-920-0010-01 Prototyping Products 7” Blue Jumpers
872-920-0011-01 Prototyping Products 0.1 Spacing Sing Row Headers
872-920-0019-01 Prototyping Products 12” Green Jumpers (cut in half)

Digikey:
700-00012-ND BREADBOARD SOLDERLESS
490-2884-ND TRIM pot 25K 25 turns


General:
Chassis (optional)
4-40 screws, nuts
3/8” 4-40 hex spacers
220 ohm resistors, 1/10 W
1K ohm resistors, 1/10 W
LEDs
Switch
R/C Servo


Appendix

Image

Fig 18 Chassis CAD Drawing


Files:

http://www.marconettengineering.com/MadHatterExperiments_files/madhatterled1.bas

http://www.marconettengineering.com/MadHatterExperiments_files/madhatterswitch2.bas

http://www.marconettengineering.com/MadHatterExperiments_files/madhatterpot3.bas

http://www.marconettengineering.com/MadHatterExperiments_files/madhatterServo4.bas

_________________
Visit:
http://groups.yahoo.com/group/SherlineCNC/
http://tech.groups.yahoo.com/group/HexapodRobotIK/


Top
 Profile  
 
 Post subject: Re: Mad Hatter Experiments
PostPosted: Wed May 11, 2011 10:29 pm 
Offline
Site Admin
User avatar

Joined: Thu Mar 01, 2001 7:00 pm
Posts: 1316
Location: Temecula, CA
Just to let everyone know, Alan got one of the first pre-production units. The production boards have short caps so they don't interfer with shield boards.

_________________
Tech Support
Basic Micro - Robotic Technology Evolved


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

phpBB SEO