Showing posts with label micro:bit. Show all posts
Showing posts with label micro:bit. Show all posts

Monday, 13 July 2020

Dancing Kitronik's Game Zap - reacts to music

You will glad to hear this is only a short post.  

In an earlier post, Build a Disco Cube:bit that reacts to music; the vibrations of music, makes the cube sitting on a speaker with the volume pushed to 11 (just to test it of course) react to the music. The accelerometers values in the micro:bit, in the three axis, are feedback to change the neopixels colour. Simple but good fun.




With some very minor (and I do mean minor) changes it works on the Kitronik's Game Zap - eight pixels are altered at a time instead of five but apart from that nothing more. The code in python is shown below:

from microbit import *
import neopixel, random

np = neopixel.NeoPixel(pin0, 64)

while True:
    for pxl in range (3,64, 8):
        rd=int(abs(accelerometer.get_x())/20)
        gr=int(abs(accelerometer.get_y())/20)
        bl=int(abs(accelerometer.get_z())/20)
        np[pxl] = (rd, gr, 0)
        np[pxl+1] = (rd, gr, 0)
        np[pxl-1] = (rd, gr, 0)
        np[pxl+2] = (rd, gr, 0)
        np[pxl+3] = (0, gr, rd)
        np[pxl-2] = (0, gr, rd)
        np[pxl-3] = (rd, 0, 0)
        np[pxl+4] = (0, gr,0)

        np.show()


I was impressed with a few tweaks it worked! Please feel to share and copy, if this useful to you please share in the comments.




All opinions in this blog are the Author's and should not in any way be seen as reflecting the views of any organisation the Author has any association with. Twitter @scottturneruon

Tuesday, 3 April 2018

How to produce a Microbit neural network

This is really part two of a set of post in response to a question from Carl Simmons (@Activ8Thinking) concerning building a micro:bit simple neuron. In the previous post a single neuron was produced. This post looks at producing a network of neurons, ie. neural network; looking to solve the problem that a single neuron can't solve, making an Exclusive OR gate (XOR)


1. Quick Overview
1.1 The neuron itself

  • Inputs are going to be binary
  • Weighted sum is bias+W1*input1+w2*input2
  • If weighted sum>=0 then the output is True (T on the LEDs) or '1'
  • If weighted sum<0 then the output is False (F on the LEDs) or '0'
1.2 The XOR
Essentially for the two input case if the two inputs are different then the output is True.

The figure below shows the arrangement of the connections; pin 2 is the output of the neurons. The two micro:bits/neurons on the left of the picture taking in the two inputs, the same inputs go to these two neurons; the output from these neurons are the two inputs to the output neuron on the right.




figure 1


The micro:bit objects used in Figure 1 were produced using the micro:bit Frtzing diagram available at https://github.com/microbit-foundation/dev-docs/issues/36 thanks to David Whale (@whalleygeek ) for these.




2. Neuron 1
This is the top left neurone in figure 1. This neurone is set to produce an output of TRUE (pin 2 going high) when the first input goes low and the second input goes high. The code for it is shown below.





3. Neuron 2
This is the bottom left neuron in figure 1. This neurone is set to produce an output of TRUE (pin 2 going high) when the first input goes high and the second input goes low. The code for it is shown below.





4. Output Neuron
Neuron 1
This is the right-hand neurone in figure 1. This neurone is set to produce an output of TRUE (pin 2 going high) when either inputs (outputs from neurons 1 and 2) goes high - in other words acting as an OR gate . The code for it is shown below. 

The overall effect is when the two inputs to the network are high/TRUE then the output of the network (this neuron) is TRUE.




5. In Action
The wiring is messy but the effect is possible to see in these images. The top neuron is the output neuron.
figure 2: inputs to the network (input 1 low and input 2 high)
Figure 3: inputs to the network (input 1 high and input 2 low)

figure 4: inputs to the network (both inputs the same)
6. Room for expansion
The neurons were 'trained' in this case by selecting the weights by hand, an improvement would be to get them to learn. How to do this on a micro:bit takes a bit more thinking about, but I would be interested in seeing how others solve that problem.




All opinions in this blog are the Author's and should not in any way be seen as reflecting the views of any organisation the Author has any association with. Twitter @scottturneruon

Friday, 2 March 2018

Microbit Neuron - producing a single neuron using a microbit

This post is in response to a question from Carl Simmons (@Activ8Thinking) about has anyone built a microbit simple neuron.


Quick Overview

  • Inputs are going to be binary
  • Weighted sum is bias+W1*input1+w2*input2
  • If weighted sum>=0 then the output is True (T on the LEDs) or '1'
  • If weighted sum<0 then the output is False (F on the LEDs) or '0'



First attempt - A simple gate using the buttons A and B
So first attempt uses the A and B buttons on the Microbit as the two inputs and it produces T for true and F for false on the LEDs. So the weights produce an AND if the bias is changed from -2 to -1 you get an OR.





More Physical Solution for Single Neuron

So in this case the buttons are removed and P0 and P1 formed the inputs the weights are the same as in the previous example with the bias of -2 being used to produce a AND gate. Programming-wise this is a simpler solution than the previous one, no converting button presses into inputs.




Figures below show the 'neuron' in action.

First, one shows the case when both inputs are '0' ie. not connected to 3v connection. The output is False (F on the LEDs)


This figure shows when only one input is '1', the output is False.



Finally what happens when both inputs are '1', the output goes to True (T on the LEDs).




Where next?
Adapting the code so it produces a digital output and then combining them into a small network to solve a problem that a single neuron can't do the Exclusive OR (XOR).



All opinions in this blog are the Author's and should not in any way be seen as reflecting the views of any organisation the Author has any association with. Twitter @scottturneruon

Monday, 7 August 2017

kitronik :Move mini buggy (Python control of LEDs)

In two previous posts I looked at control the :Move buggy using JavaScript Blocks or Python. In this post we are going to look at controlling the LEDs using Python (or more accurately micropython).

Pin 0 controls the LEDs, they are based on5  NeoPixel compatible, RGB, addressable LEDs; so the Neopixel protocols (and library for Neopixels) can be used. 



Code First five colours of the rainbow. The array lig holds the RGB settings for the rainbow colours (more details on the RGB colours can be found at Lorraine Underwood's Halloween Cloud project). In the code below, the five LEDs have a different colour allocated to them.

from microbit import *
import neopixel

np = neopixel.NeoPixel(pin0, 5)
lig=[[255,0,0],[255,127,0],[255,255,0],[0,255,0],[0,0,255],[75,0,136],[139,0,255]]
while True:
    np[0] = lig[0]
    np[1] = lig[1]
    np[2] = lig[2]
    np[3] = lig[3]
    np[4] = lig[4]

    np.show()


Code to cycle through the rainbow
from microbit import *
import neopixel

np = neopixel.NeoPixel(pin0, 5)
lig=[[255,0,0],[255,127,0],[255,255,0],[0,255,0],[0,0,255],[75,0,136],[139,0,255]]
count1=1
count0=0
count2=2
count3=3
count4=4
while True:
        np[0] = lig[count0]
        if count0>=6:
            count0=0;
        else:
            count0=count0+1
        np[1] = lig[count1]
        if count1>=6:
            count1=0;
        else:
            count1=count1+1
        np[2] = lig[count2]
        if count2>=6:
            count2=0;
        else:
            count2=count2+1
        np[3] = lig[count3]
        if count3>=6:
            count3=0;
        else:
            count3=count3+1
        np[4] = lig[count4]
        if count4>=6:
            count4=0;
        else:
            count4=count4+1
        np.show()

        sleep(500)


All opinions in this blog are the Author's and should not in any way be seen as reflecting the views of any organisation the Author has any association with. Twitter @scottturneruon

kitronik :Move buggy (Python controlled servos)

In a previous post I looked at controlling the Kitronik :Move buggy using Javascript based blocks. In this short post I will show controlling the servos of the micro:bit based :Move buggy with Python.

Control is via pin1(left motor) and pin2 (right motor) and the motors have to be driven in opposite directions to move forward or backwards. The direction of the motors is controlled by the analogue value written to the pins; pinX.write_analog(180) - anticlockwise or pinX.write_analog(1) - clockwise (pinX.write_analog(0) - stops the motor). Setting the analog_period seems to work at 20ms; this was found by experiment, discussed in a previous post.

So the initial code below sets up the moves for forward, backward, turn left, turn right all controlled with a move for so many milliseconds.

Code 
from microbit import *

pin1.set_analog_period(20)
pin2.set_analog_period(20)

def forward(N):
    pin1.write_analog(180)
    pin2.write_analog(1)
    sleep(N)
    
def backward(N):
    pin1.write_analog(1)
    pin2.write_analog(180)
    sleep(N)

def turnLeft(N):
    pin1.write_analog(1)
    pin2.write_analog(1)
    sleep(N)

def turnRight(N):
    pin1.write_analog(180)
    pin2.write_analog(180)
    sleep(N)

while True:
    forward(1500)
    backward(1500)
    turnLeft(1500)
    turnRight(1500)   

I would recommend running the :Move buggy on a surface that isn't smooth, I found the wheels slipping on a smooth surface. This is a really nice little robot to play with.

All opinions in this blog are the Author's and should not in any way be seen as reflecting the views of any organisation the Author has any association with. Twitter @scottturneruon

Wednesday, 26 July 2017

kitronik :Move mini buggy (JavaScript blocks)

Finally got around to building add playing with the Kitronik :Move https://www.kitronik.co.uk/5624-move-mini-buggy-kit-excl-microbit.html (see below - I decided to put the green sides on the outside - just to be different). One of its features is a vertical set of holes for a pen to be placed in.


Add the blocks (found at https://github.com/KitronikLtd/pxt-kitronik-servo-lite) in blocks editor (https://makecode.microbit.org/) to control the motors. You can do the same thing with writing to the pins, those instructions come with the build instructions, but using the extra blocks  is a little easier to understand. Also add the package for neopixels (type in neopixels in the search box to find them). Two very good tutorials I found useful to start with can be found at:









1. Motor example
I wanted it so that press A on the Micro:bit the robot goes turns right, goes forward, goes back and turns left. 






A stop block does need to be included, without it the :Move will continue moving. The wheels I found can slip on some surfaces reducing the precision, but still fun to play with.

2. At the start and stopping.
I want to use the motors and the 'pixels', but I want to have a known starting position for the motors and set the turning speed; this was possible using the blocks (see below). The pixels are set at this point on pin P0 (see below) as well. 

To stop both the motors and cycling of the pixels - pressing buttons A+B together was set up to this.




3. Rainbow on the pixels.
On pressing button B the pixels rotate through a range of colours.




4. Summary
This is great fun. Having the set of blocks adding for the servos means it is a bit simpler to work with. 








All opinions in this blog are the Author's and should not in any way be seen as reflecting the views of any organisation the Author has any association with. Twitter @scottturneruon

Monday, 1 May 2017

Microbit Remote Control CBiS Car

I wanted to contol the CBiS micro:Bit Car via gestures whilst holding another micro:Bit (see Figure 1)

I went for:
- Button A in combination with moving the micro:bit left or right, moves 'Car' forwards or backwards;
- Button B in combination with moving the micro:bit rotated forward or backwards, turns the 'Car' left or right;

Perhaps not the most logical combination but fun.
Figure 1: CBiS micro:bit car and 'controller' micro:bit

The inspiration from this remote car idea came from four sources
- CBiS Education site and seeing them demonstrating it;
- Technology with Save Us Micro:bot Radio Control website ;
- DrBadgr blog on the Lunch Box robot;
- A twitter conversation


The approach taken is simple; the Controller micro:bit has the following operations (see Figure 2 for the PXT code)

  • Buttons A+B together send '0' out by a radio protocol;
  • Button A with changes in the x-direction send '1' or '2';
  • Button B with changes in the y-direction send '3' or '4' ;




Figure 2: Remote Control 
For the code go to: Remote Control - the images produce on the micro:bit, are there as a bit of fun (they have no meaning) and are different for each action.


The control on the 'Car' turns the received numbers  (sent from the controller) into forward, backward, turning motions and stop. The PXT code can be found at Motor Control.


Figure 3: Car Control

The control is basic but fun (well I enjoyed it). There is a lot of scope for improvement and adaption. Please feel free to adapt any of the code and it would be great to hear what others do via the comments.






All opinions in this blog are the Author's and should not in any way be seen as reflecting the views of any organisation the Author has any association with. Twitter @scottturneruon

Sunday, 9 April 2017

CBiS Education Micro:Bit based Robot Car

At PiWars 2017 (1st-2nd April 2017), thanks to the generosity of CBiS Education, I now have one of their BBC micro:bit RobotCar . It is a sturdy bit of kit, encased in an aluminum chassis with a clear acrylic screen - it feels substantial when you pick it up. 

It is based around fours motors, control by a Micro:Bit, via L298N based motor controller/shield. Batteries power, 8 AAs, the motors and a Lithium powerbank to power the Micro:Bit - all included. 

More information about the technical details and example software can be found on their site https://www.cbis.education/robotic-car-kit# including further details on the L298N based motor controller/shield, which I found useful for programming it. 




I have experimented briefly with programming it in Python (micropython), getting it to it move forward, backward; to the right and left, using the Mu editor. 

The code is shown below for those who want to try it:

from microbit import *


def forward(n):
    pin13.write_digital(1)
    pin16.write_digital(1)
    sleep(n)
    
def backward(n):
    pin14.write_digital(1)
    pin15.write_digital(1)
    sleep(n)

def stopit(n):
    pin13.write_digital(0)
    pin14.write_digital(0)
    pin15.write_digital(0)
    pin16.write_digital(0)
    sleep(n)

def spinLeft(n):
    pin13.write_digital(1)
    pin15.write_digital(1)
    sleep(n)

def spinRight(n):
    pin14.write_digital(1)
    pin16.write_digital(1)
    sleep(n)


while True:
    forward(500)
    stopit(1000)
    backward(500)
    stopit(1000)
    spinRight(1000)
    stopit(1000)
    spinLeft(1000)
    stopit(1000)


This thing is quick. I like the feel of it, you pick it up and it doesn't feel like it is going to fall apart; because it comes all assembled - including batteries, it is ready to go. I look forward to trying some more ideas on it.



All opinions in this blog are the Author's and should not in any way be seen as reflecting the views of any organisation the Author has any association with. Twitter @scottturneruon

Sunday, 2 April 2017

Micro:bit, PXT, Micro:pixel and Rainbows

Continuing to play with PXT (http://pxt.microbit.org) and the Micro:bit, I wanted to try this in combination with a Proto-Pic Micro-pixel board with its 32 neopixel LEDs.

The routine (shown in the screenshot below) use the Neopixels package (use the add package option on the menu to add it) to do two things:

  • On pressing button B - cycle through the colours and shift the colour to the next pixel producing a shifting pattern.
  • On pressing button  A - the pixels are cleared one by one.

Code is shown above, and thank you to Jonathan "Peli" de Halleux (@pelikhan) for pointing out the redundant code I had left in, it is appreciated.

The video below shows the system in action:




You can try the code out in the simulator below:




All opinions in this blog are the Author's and should not in any way be seen as reflecting the views of any organisation the Author has any association with. Twitter @scottturneruon

Tuesday, 28 March 2017

Micro:bit Hot-Cross Buns with Microsofts's PXT (with video)




Just been playing with Microsoft's PXT language for the Micro:bit - so had a go at 'Hot Cross Buns'. Nice to see it shows in the simulator the wiring connections to the speakers.


Hot-Cross Buns - runs on either button A or B being  pressed. 
By the way if the sound is anoying you, press the stop button on the simulator below.






All opinions in this blog are the Author's and should not in any way be seen as reflecting the views of any organisation the Author has any association with. Twitter @scottturneruon

Sunday, 29 January 2017

my robot BETT2017

I will start with a confession, I only had about 2 1/2 hours at BETT 2017 due to external time pressures so to say I didn't yet a chance for a good (or even a bad) look around is an understatement; so I am not reviewing the show just a few notes on what I did manage to see.


STEAM Village
First and mostly, it was great to talk to so many people, only few I had met face to face previously, about robots, micro:bits, Raspberry Pis and coding. Most of this happen in the relatively small (compared to the event space) STEAM village and nearby stalls. It was great to see the strong presence of both Raspberry Pi and Micro:Bit Foundation, along the variety of different activities and example usage of both, with Code Club (I know it is part of Raspberry Pi Foundation) there was well. This was all alongside some other companies

Four of these stuck in my mind.

1. DFRobot (https://www.dfrobot.com/) with their range of Arduino-based robots and non-programmable kits. The two kits that caught my eye was the FlameWheel robotics kit (to see more on this go to https://www.dfrobot.com/index.php?route=DFblog/blog&id=563) and the Insectbot kit (see the video below). As an aside, I recently got one of their new designs Antbo through a crowdfunding offer https://www.indiegogo.com/projects/antbo-an-insect-robot-anyone-can-build-steam-diy/ (more this in a future post)




2. School of Code (http://schoolofcode.co.uk/) with their web-based coding but with the emphasis on collaborative coding was great to see. Have at go for yourself at their character building example http://www.schoolofcode.io/game-avatar, done as part of the Hour of Code, it is good fun.


3. It was great see GitHub were there was as well, and they couldn't be more generous with the stickers and other materials. I wish I had more time to talk to them.

4. Last but not least was CBiS Education (http://www.cbis.education/) with an extremely tall model of their DIMM robot. I think their products are interesting, here are a few earlier posts about projects with their products Robot Arm and Python, UFO talks to Robot part 1, UFO talks to Robot part 2).


Outside of the STEAM village
There were two stands that particularly caught my attention.

Robots In Schools Ltd (I wish I own that name) with their Edbot package - a single Edbot but networked so the teacher can share access to the robot by assigning control to the students machine. I really liked this idea. For more details go to: https://robotsinschools.com/inclassrooms/.

Second was Ubtech (http://ubtrobot.com/) with their Alpha 1 and 2 (more on Alpha 2 in a future post). What I found most interesting was their ideas of potentially embedding Raspberry Pi or Arduino inside their Alpha 1 for a more open source solution.


Would I go back next year? 
Yes please, but I just want a day or more there though next time.  

All opinions in this blog are the Author's and should not in any way be seen as reflecting the views of any organisation the Author has any association with. Twitter @scottturneruon

Friday, 30 December 2016

Neuron Controlled Edge follower updated

In the last post experimentation with an artificial neuron controlling the Bit:Bot to follow the edge of a line (it follows the left-hand side of the line).




More details can be found in the previous post.The neurons (well two separate ones, S1 and S2) are produced using weighted sums - summing the weights x inputs [ right-hand sensor (rs) and left-hand sensor (ls)] plus a bias for each neuron in this case w[0] and w[3].
    net=w[0]+w[1]*rs+w[2]*ls      
    net2=w[3]+w[4]*rs+w[5]*ls


If weighted sum >=0 then its output 1 otherwise 0


What actual causes S1 to be either 1 or 0 is all defined by a set of weights w (three for the first neurone, S1,  three for S2).

w=[0,-1,1,-1,1,-1]


Modifications to the code in the last post have been around fine tuning the values in converting the outputs of the two neurons S1 and S2 into actions as shown below.
    if s1==1 and s2==1:
        forward(20)   
    elif s1==0 and s2==1:
        forward(15)
        right_turn(25)
    elif s1==1 and s2==0:
        forward(15)
        left_turn(25)       
    elif s1==0 and s2==0:
        backward(5)

The functions for forward, right_turn, etc are defined elsewhere.


To change the function of the system, change the values in wThe complete code is shown below.


Code
from microbit import *
import neopixel, random, array

w=[]  

def forward(n):
    pin0.write_analog(551)
    pin8.write_digital(0) 
    pin1.write_analog(551)
    pin12.write_digital(0)
    sleep(n)
    
def backward(n):
    pin0.write_analog(551)
    pin8.write_digital(1) 
    pin1.write_analog(551)
    pin12.write_digital(1)
    sleep(n)
    
def right_turn(n):
    pin0.write_analog(511)
    pin8.write_digital(0) 
    pin1.write_analog(511)
    pin12.write_digital(1)
    sleep(n)
    
def left_turn(n):
    pin0.write_analog(551)
    pin8.write_digital(1) 
    pin1.write_analog(551)
    pin12.write_digital(0)
    sleep(n)
       
w=[0,-1,1,-1,1,-1]

while True:
    ls= pin11.read_digital()
    rs= pin5.read_digital()
    
    net=w[0]+w[1]*rs+w[2]*ls
    net2=w[3]+w[4]*rs+w[5]*ls

    if net>=0:
        s1=1
    else:
        s1=0

    if net2>=0:
        s2=1
    else:
        s2=0
   
    if s1==1 and s2==1:
        forward(20)   
    elif s1==0 and s2==1:
        forward(15)
        right_turn(25)
    elif s1==1 and s2==0:
        forward(15)
        left_turn(25)       
    elif s1==0 and s2==0:
        backward(5)


Video of it in action:






Please feel free to use the code and improve on it, and I would especially welcome the seeing the improvement through the comments.



All opinions in this blog are the Author's and should not in any way be seen as reflecting the views of any organisation the Author has any association with. Twitter @scottturneruon

Remote Data Logging with V1 Microbit

In an earlier post  https://robotsandphysicalcomputing.blogspot.com/2024/08/microbit-v1-datalogging.html  a single microbit was used to log ...