Showing posts with label motor. Show all posts
Showing posts with label motor. Show all posts

Tuesday, July 19, 2016

Toy crane controlled from Raspberry Pi Zero with SenseHat using Scratch


Since the micro:bit was launched one of the demos that I kept seeing was the toy crane controlled using the motion sensing in the micro:bit.  The kids loved it and was very interactive.

Since the SenseHat on the Raspberry Pi has the motion sensors built in I thought this would a good project for a Raspberry Jam.

So, off I went to Home Bargains and bought a crane. From the picture, definitely the right age for me.  This crane is normally controlled using two levers on the hand controller. It can rotate and raise/lower the bucket.

Didn't come with the hard hat
First order of business was to figure out the wiring and cut the cable as the final build would be battery powered to work the same as the micro:bit version.


Cut the red wire

From above the wiring is:
VCC - Red wire
GND - Brown wire (not black)

Rotate motor 
Orange and Yellow

Crane lift motor
Blue and Green.

For turning and crane the direction it goes depends on how you wire it up and how your code works so some adjusting may be needed later.

To make sure I had this right I tested by touching Red to Orange and Brown to Yellow. Crane rotated one way. Swapped wires around and crane rotated the other way.
Red to Blue and Brown to Green, Bucket went down. Swapped wires around and bucket went up.

Wiring confirmed and tested.

As the Raspberry Pi isn't designed to control motors directly I needed a small motor controller.
The L9110s looked perfect for the job. Smaller than the L298N that I usually use so would be easier to accommodate in the final box.

L9110s motor controller
The 2 terminal blocks on the left in the image are for the motors.
On the right are the control pins and power.
Top 2 connections are control for Motor A
Bottom 2 connections are control for Motor B
Then in the middle are VCC and GND

NOTE: A really important thing is to make sure when using multiple boards that all the GND lines are tied together so that all the voltages have the same base reference.  Otherwise strange things can happen.

I wired up the motors. Turning uses GPIO 5 and GPIO 6 on the Raspberry Pi and Motor B on the L9110s while lifting uses GPIO 27 and GPIO 17 and Motor A.

Again did some simple code to see if it would work from Scratch. Initially manually moving with the keyboard.  It worked great and I brought it along to the Egham Jam in April 2016.  As the organiser of the Jam I was a bit delinquent in taking pictures so the only one I have is of the crane, bottom left in the booth of the car before I went to the event. I promise the kids loved it and it was a massive hit, really. Actually, I had a different project called ZeroBall that was finished so it took most of my time.

Bottom leftis the crane
There were two technical reason I didn't have it all set up with motion sensing and coded in Scratch for the Jam.

  1. The SenseHat covers all the GPIO pins meaning I couldn't get at the pins to attach the wires for the motor controller.  So, I could get the readings from the SenseHat but couldn't control the motors.
  2. The Scratch at the time had a problem whereby it didn't support AddOn board. 


The first problem was solved with Stacking Headers and great tutorial from Keith's Pi Tutorials even has a video.
Also, a great reference site for Raspberry Pi board pin usage is pinout.xyz.  They have loads of boards listed and this is where I got the details for the pins used on the SenseHat

Stackable Headers. Note the high tech blutac for holding it all in the tub


The second problem of Scratch not working at all with AddOn boards was reported and fixed in the May 2016 Raspbian update.

With the purchase of Stackable Headers from The Pi Hut and a freshly imaged SD card both of these technical problems were overcome and the way forward was sorted for the SenseHat to be used to control the crane.

Since I wanted it to be battery powered the Raspberry Pi Zero was the obvious choice as it is low power. Only thing is Scratch is a GUI program and so I needed a desktop environment to run it (If you can run Scratch code without a GUI I'd love to know, but I suspect it kind of defeats the purpose of a drag and drop interface if you run it from the command line)

To overcome this I used a USB wifi dongle to connect over the network to the PiZero and then on a laptop used RealVNC to get a desktop. Since this was going to be shown at the first Wimbledon Raspberry Jam I didn't know if I would have a wifi network to connect to so I brought my own in the shape of a VoCore. A one inch cubed wireless router running openWRT.  I backed this as a crowdfunding thing a while back and all I've used it for it to create a local wifi hotspot.  At the Jams I can even power it from a powered Raspberry Pi USB port.

Side Notes:
I think the number is the manufacturing order of the original batch and I've never seen one with a number lower than 26.
The VoCore is only the top layer. The rest is an add-on that gives you USB, Ethernet, microUSB power socket and a microSD slot.  IT also has it's own GPIO pins so can be used for embedded projects.

Vocore. Basically, the guts of a wifi router
Obviously, as this was going to be standalone it had to be battery powered. I'd been picking up these 18650 Lithium Ion batteries in Poundworld and thought 4 of these in a case would make a decent power supply for the day.  No idea why when I cracked them open one was pink.  The battery case was from eBay and it all snapped together really easily.
The case has 2 USB ports for power so could power the Raspberry Pi and the VoCore at the same time.

Batteries in their original cases

Batteries in their new case


I mentioned above that all the Grounds need to be tied together.  In this instance I have 3 circuits that need to all have the same GND. Crane, L9110s and Raspberry Pi Zero (The SenseHat is take care of through the Pi Zero)
I tied the Crane GND (Brown wire directly to Pin 39 on the Raspberry Pi, then the GND pin on the L9110s was tied to Pin 6 on the Raspberry Pi.  In this way all 3 had a common GND.

It's good to be aware that in a circuit all GND can usually be treated as the same point. Especially for such low power and low frequency signals.  If this was a high power high frequency circuit then the trace length between the different GND points could case harmonics.  I cannot think of a project using the Pi that I would do where the length of the trace between GND pins would be a problem


So, now I have power, local network, a way to run Scratch so people can see the code and all the bits wired up. Last of all was the actual Scratch code.

From the image you'll see the code isn't very complex.  The main thing I'd to figure out was which one of the sensors I needed to read. It's the Accelerometer.
I believe the range is -4095 to + 4095 on each axis.
When the program is run a base "flat' reading for Lifter and Turning is taken so any movement in the sensor doesn't carry from one person to the next.  This is what the middle block of code does.
The block of code on the right is there if it all goes wrong and I can just press [space] to turn off the motors.

Th middle block is where the magic happens.  It checks if Accelerometer X is 1000 less or 1000 more than the flat reading for Lifter and/or Turning and then activates the appropriate motor to either turn the crane or raise/lower the bucket.

Not shown below is a second costume for the crane that just says "Put Pi on a flat surface" at the start of the calibration.


If you're not up for trying to copy the code from the image you can download the Scratch code from GitHub.

To make it hand held I stuffed all the bits in to a plastic Chinese takeaway box.  You can see the L9110s on the left, wifi dongle on a short microUSD to USB cable and the SenseHat with the Stackable Headers so the wires for the motor controller can could be added.

Finally, here is a boy totally engrossed in playing with the crane at the Wimbledon Jam.  It all ended really well.

Hours of fun transporting monkey from one place to another









Friday, October 9, 2015

9th Egham Jam – Show and Tell Competition – Prizes donated by 4tronixs


I think we might be coming to the end of the Prize announcements.
So, last but not least are the Prizes donated by 4tronixs.

Robot lovers Prizes from 4tronixs

These prizes are especially situated to the robots lovers.

Each Prize includes:

The PlayHat includes a 3x3 full colour neopixel LED matrix, 4 big buttons and a buzzer.
Great for programming with the Raspberry Pi. This is just begging out for a game of Simon to be created.

PiRoCon and iBoost64 This board is great. Te PiRoCon  plugs right on top of the Raspberry P and gives you all the additional electronics needs to drive motors.
Motors are fun things. When they start they draw a lot of  power (think of train pulling out of the station. It needs far more power to get going then it does to keep itself moving.  Same thing for motors).  This mean if you want to drive motors you need extra circuitry to do it safely without damaging your Raspberry Pi.  The PiRoCon is one of the best solutions for this for the Raspberry Pi.

In addition to motors drawing a lot of power when they start and stop they can also affect the voltage and current of electronics drawing from the same power supply.  This can cause difficulties in getting good readings or accurate control.  This is where the iBoost64 comes in.  It cleans up the signals and makes them good without affecting the rest of the circuit.  Ideal for robots where there can be a lot of stopping and starting of motors as well as reading of external sensors for line following or object avoidance.

With the combination of the PiRoCon and iBoost64 you have the electronics to get that robot built.
If working in Scratch the current ScratchGPIO library from Simon Walters supports the PiRoCon.

If you have robots you will need power.  The final 2 boards are a 1 way and 4 way charger board for Li-Po batteries.  These are great batteries for robotic as they store lots of energy and give out a consistent voltage.  Cheap batteries have a power curve that means they very quickly drop from the maximum voltage which can cause lots of problems with motors.  Li-Po batteries are the ones used in high quality remote control cars.
Unfortunately you can't just plug a Li-Po battery into your normal battery charger due to the way they work so custom chargers are usually provided or need to be purchased with the battery.  These great little boards mean you can use a standard 5V microUSB power supply to charge the batteries.  This is the same type of power supply used for the Raspberry Pi, meaning if you are building a Raspberry Pi based robot you will already have the correct power supply.
There are 2 different boards included.  A 1 way, so it can charge 1 battery and a 4 way which can change 4 batteries at the same time.  Giving you more than enough power for your laser guided, GPS tracking, missile shooting, web cam streaming, grabber, digger, flame thrower robot. Or maybe just a line following robot.

From the picture you can see they also provided some pens.

Thank you 4tronixs for the prizes.







Monday, January 12, 2015

Robot Club - first session completed

Link to Previous Post on Preparing for the Club with details on Partner Used: http://winkleink.blogspot.co.uk/2014/11/preparing-for-2015-after-school.html


Today we had the first session of the new robot club I'm running at my kids primary school.
It is for year 6, so 11-12 year olds.

The club is based on the parts from the previous post. http://winkleink.blogspot.co.uk/2014/11/preparing-for-2015-after-school.html

Today the kids came in and there were 3 from last year, so 7 new which is great.
Also, the gender mix is 4 boys and 6 girls.  Sounds very positive from a gender equality then heard the robot club is clashing with football, so expect there are many boys already committed to football.

Still it was a great first session.

The goal for today was to build the chassis.  I wanted to give the kids a bit of a challenge so we worked it out together using the Chinese instructions and my previously built chassis.

A lot of bolts going in the wrong way round and parts in upside down. So, each team had to take their bits apart multiple time.

As we went through the hour I expected the kids to lose heart with the every increasing times they had to remake the chassis.  But, I was delighted that by the end they were very excited to have it built and really wanted to do more.  We had to stop as parents were waiting to collect the kids.

We wired up some of the motors and I showed that since the motors mirror each other than with the motors wired the same way that one wheel goes forward and the other goes backwards with the same red-positive / black-negative wiring.  I explained we can sort this out in code.

The best comment of the day from one of the girls was. "Today is the first time I don't want to leave school."

She was very curious about the 4x4 keypad, so I explained how it works.
Send a signal into each horizontal pin one after another and seeing which pin it comes out at.

Metal Note: try to bring a white board market next time.  Teachers take them away after their classes.


In preparation for next week I have to get all the chassis sorted. One of the motors had the tab ripped off so lucky I have a spare motor.

Then add the L298N H-Bridge and Arduino Nano compatible so coding can start next week.

Goals for next session.
Understand the wiring.
Code to make it go forward, backwards and turn.
Program to go around the centre section in the room.

Roll on next Monday.




Monday, November 3, 2014

Preparing for 2015 after school computer club


Over the past 2 years I have done a Code Club at my kids school using Scratch and last year included a bit of Arduino. Now in the UK computing is part of the school curriculum so the Code Club learning is now done in school time meaning there is not need for it to be done after school.
With this in mind for 2015 I've decided to ramp up the difficulty and go Arduino all the way with a robot course.

This time limited to 10 kids working in pairs as the complexity is higher and the projects will most likely be done over multiple weeks.




This will give the ability to build everything from a pre-programmed robot to a line following robot or an object avoidance robot. Also included an infrared sensor and remote so the kids can make a remote control robot.

All going well the kids will also be able to decorate their robots with unique bodies to make them more exciting.

In addition to the parts below I also have 36 rechargeable AA batteries for the club.

I'm looking forward to January already.

Below are the list of parts I plans to have for the 8-9 week club. 5 kits

ItemeBay LinkPriceQTYTotal
Robot Deckhttp://ebay.to/10NBbMM£7.485£37.40
DC Motor H-Bridgehttp://ebay.to/1GeGZyY£1.555£7.75
Arduino Nano compatiblehttp://ebay.to/1rRgTrh£2.895£14.45
5 x Breadboard 400 tiehttp://ebay.to/1EcNoZs£9.491£9.49
Line following sensorhttp://ebay.to/1wXWDKI£1.195£5.95
10 x Ultra Sonic sensorhttp://ebay.to/1GeHwRl£8.991£8.99
Infrared sensor + Remotehttp://ebay.to/1A40r0N£1.355£6.75
Wires M-Mhttp://ebay.to/1tujL19£1.991£1.99
Wires M-Fhttp://ebay.to/1tSRIKe£1.991£1.99
Wires F-Fhttp://ebay.to/1tSRRxf£2.241£2.24
Keypad 4x4http://ebay.to/1wX1r4F£0.995£4.95
£101.95

Sunday, February 23, 2014

Arduino Compatible robot for £15

I was looking for a low cost way of building a vehicle controlled by an Arduino or Raspberry Pi and eBay once again came up with the solutions.

1 x Smart Robot Car Chassis Kit  - £7.60
1 x L298N - £1.86
1 x Arduino Compatible Pro Mini - £2.53

Total Cost: £11.99

The Smart Robot Chassis comes with 2 motors and wheels.  It comes with the encoding wheels for optical encoder to measure rotation, but without the electronics.
All the parts with chassis. 


The L298N is a Motor Driver Module
That's it on the right next to the battery pack


While the Pro Mini is a small Arduino compatible board that I previously blogged about programming (http://winkleink.blogspot.co.uk/2014/01/programming-arduino-pro-mini-16mhz-5v.html) and soldering the headers (http://winkleink.blogspot.co.uk/2014/01/soldering-up-pro-mini-arduino.html)

You need a USB to serial adaptor to program it. For this adaptor you have to solder a pin to the DTR and then attach that to the RST line on the Pro Mini when programming.

I built the Chassis and attached the L298N
All the parts

Bits for the front wheel mounting
Screw the spacers to the chassis


Attach the swivel wheel to the spacers.
Note nuts not in original picture

Parts for motor/wheel mount

Use small screws to attach pillars to chassis

Parts for connecting motors

Screw motor to the outside of the pillars.
Note: make sure to have motor connectors on the inside
for easy cable management

Add the encoder wheels if you want and the actual wheels

See in front of batter holder are the two holes where
the battery holder will be bolted to.
Use 2 of the slightly longer bolts for this.

All finished.  Still needed to solder the wires to the motors.


The L298N is then wired.

+ Battery to VCC and 5V on the L298Nand RAW on the Pro Mini. The L298N appears to be OK with 6V going to the 5V line. And the Pro Mini doesn't complain about the 6V from the batteries either.
As always connect all the GNDs together

Then for each motor connect Out1 and Out2 to one Motor and Out3 and Out4 to the other motor. Depending on which way around you do the connection will decide which way the motors spins, so if this is wrong you can either swap the wires or modify the code.

Then for control from the Pro Mini I connected

IN1 to 3
IN2 to 5
IN3 to 9
IN4 to 10

The eagle eyed will have noticed these are 4 of the PWM lines so in the future I can enable speed control by driving using PWM rather than digital.


Uploaded the following Sketch tot he Pro Mini and let it rip - see video at the bottom

// motor driver using PWM

int lb = 3;
int lf = 5;
int rb = 10;
int rf = 9;


void setup() {
  // put your setup code here, to run once:

  pinMode(lb, OUTPUT); 
  pinMode(lf, OUTPUT); 
  pinMode(rb, OUTPUT); 
  pinMode(rf, OUTPUT); 
  
  digitalWrite (lb, LOW);
  digitalWrite (lf, LOW);
  digitalWrite (rb, LOW);
  digitalWrite (rf, LOW);

}

void loop() {
  // put your main code here, to run repeatedly: 
  
    digitalWrite (lf, HIGH);
    digitalWrite (lb, LOW);
    digitalWrite (rf, HIGH);
    digitalWrite (rb, LOW);

    delay(1000);

    digitalWrite (lf, LOW);
    digitalWrite (lb, LOW);
    digitalWrite (rf, LOW);
    digitalWrite (rb, LOW);

    delay(1000);

    digitalWrite (lf, LOW);
    digitalWrite (lb, HIGH);
    digitalWrite (rf, HIGH);
    digitalWrite (rb, LOW);

    delay(600);

    digitalWrite (lf, HIGH);
    digitalWrite (lb, LOW);
    digitalWrite (rf, HIGH);
    digitalWrite (rb, LOW);

    delay(1000);

    digitalWrite (lf, HIGH);
    digitalWrite (lb, LOW);
    digitalWrite (rf, LOW);
    digitalWrite (rb, HIGH);

    delay(600);
}



All worked well.
Next I need to put in place a way of controlling it remotely.
The adventure continues.

Note: This chassis and L298N should also work with a Raspberry Pi. Need to be more careful with the power and a few other bits.