Components and supplies
Power supply 12V 1A
Jumper wires (generic)
Resistor 1k ohm
Arduino Nano R3
Electromagnet 12v 25mm diameter.
Pushbutton switch 12mm
Linear Hall Effect Sensor 49E
Darlington High Power Transistor
Breadboard (generic)
1N4007 – High Voltage, High Current Rated Diode
Apps and platforms
Arduino IDE
Project description
Code
Levitator.ino
arduino
Code for Magnet Levitation toy with Arduino. Easy(No PID)
1//=========================================================| 2// Ekobots Innovation Ltda - www.ekobots.com.br | 3// Juan Sirgado y Antico - www.jsya.com.br | 4//---------------------------------------------------------| 5// Program Magnetic Levitator - 2016/02/06 | 6// All rights reserved 2016 | 7//=========================================================| 8int anaPin = 1; // Arduino Analogic Pin 1; 9int digPin = 5; // Arduino Digital Pin 5; 10int subPin = 7; // Arduino Digital Pin 7; 11int addPin = 8; // Arduino Digital Pin 8; 12// 13int anaVal = 0; // Analogic Read(Hall Sensor) Value; 14boolean digVal = 0; // Digital Write(Electromagnet) Value; 15// 16int levVal = 228; // Levitation Poit Value; 17int dlyVal = 250; // Delay Value Micro Seconds; 18//---------------------------------------------------------| 19void setup() 20{ 21 // Levitator initialization Begin; 22 Serial.begin(57600); 23 Serial.println("Levitator by JSyA"); 24 Serial.println("Starting..."); 25 // Digital Pins Work Mode Setup; 26 pinMode(digPin, OUTPUT); 27 pinMode(subPin, INPUT_PULLUP); 28 pinMode(addPin, INPUT_PULLUP); 29 // Levitator initialization End; 30 Serial.println("Started."); 31} 32//---------------------------------------------------------| 33void loop() 34{ 35 // Hall Sensor Read (Magnetic Field Intensity); 36 anaVal = analogRead(anaPin); 37 // Increase The Value Of Levitation Point; 38 if (digitalRead(addPin) == LOW) 39 { 40 levVal++; 41 value_log(); 42 delay(250); 43 } 44 // Decrease The Value Of Levitation Point; 45 if (digitalRead(subPin) == LOW) 46 { 47 levVal--; 48 value_log(); 49 delay(250); 50 } 51 // Check the Levitation Point; 52 if (anaVal < levVal) 53 { 54 digVal = LOW; 55 } 56 else // if (anaVal > levVal) 57 { 58 digVal = HIGH; 59 } 60 // 61 // Turn ON/OFF The Electromagnet; 62 // With Base on Sensor Value and the Levitation Point; 63 digitalWrite(digPin, digVal); 64 delayMicroseconds(dlyVal); 65} 66//---------------------------------------------------------| 67void value_log() 68// Analogic/Digital/Levitation Values Print; 69{ 70 // Show the Hall Sensor Value; 71 Serial.print("anaVal=["); 72 Serial.print(anaVal); 73 Serial.print("]-"); 74 // Show the Electromagnet state On=1/Off=0; 75 Serial.print("digVal=["); 76 Serial.print(digVal); 77 Serial.print("]-"); 78 // Show the Levitation Point Value; 79 Serial.print("levVal=["); 80 Serial.print(levVal); 81 Serial.println("];"); 82} 83//=========================================================| 84
Levitator_PID.ino
arduino
Code for Magnet Levitation toy with Arduino. Hard(PID version)
1//=========================================================| 2// Ekobots Innovation Ltda - www.ekobots.com.br | 3// Juan Sirgado y Antico - www.jsya.com.br | 4//---------------------------------------------------------| 5// Program Magnetic Levitator PID - 2016/10/06 | 6// All rights reserved 2016 | 7//=========================================================| 8int anaPin = 1; // Arduino Analogic Pin 1 9int digPin = 5; // Arduino Digital Pin 5 10int subPin = 7; // Arduino Digital Pin 7 11int addPin = 8; // Arduino Digital Pin 8 12// 13int anaVal = 0; // Analogic Valie 14int digVal = 0; // Digital Value 15// 16int levVal = 262; // Levitation Value 17int dlyVal = 10; // Delay Value 18// 19float timVal = 0; // Time Value 20//---------------------------------------------------------| 21// PID Values 22float setpoint = 0; 23float measured_value = 0; 24float output = 0; 25float integral = 0; 26float derivative = 0; 27float error = 0; 28float previous_error = 0; 29float dt = 0.1; 30float Kp = 1.0; 31float Ki = 0.1; 32float Kd = 0.01; 33//---------------------------------------------------------| 34void setup() 35{ 36 // Levitator initialization Begin; 37 Serial.begin(57600); 38 Serial.println("Levitator by JSyA"); 39 Serial.println("Starting..."); 40 // Digital Pins Work Mode Setup; 41 pinMode(digPin, OUTPUT); 42 pinMode(addPin, INPUT_PULLUP); 43 pinMode(subPin, INPUT_PULLUP); 44 // 45 timVal = millis(); 46 setpoint = levVal; 47 // Levitator initialization End; 48 Serial.println("Started."); 49} 50//---------------------------------------------------------| 51void loop() // PID 52{ 53 // Hall Sensor Read (Magnetic Field Intensity); 54 anaVal = analogRead(anaPin); 55 // PID calculations 56 measured_value = anaVal; 57 error = setpoint - measured_value; 58 integral = integral + error * dt; 59 derivative = (error - previous_error) / dt; 60 output = (-Kp * error) + (-Ki * integral) + (-Kd * derivative); 61 previous_error = error; 62 // Final value setup 63 digVal += output; 64 // Check the value for levitation point; 65 if (digVal < 0) digVal=0; 66 if (digVal > 255) digVal=255; 67 // Increase/Decrease the value for Electromagnet; 68 // With Base on Sensor Value and the Levitation Point; 69 analogWrite(digPin, digVal); 70 // Show log values for debug; 71// if((millis()-timVal) > 500) 72// { 73// value_log(); 74// timVal = millis(); 75// } 76 // Increase The Value Of Levitation Point; 77 if (digitalRead(addPin) == LOW) 78 { 79 setpoint++; 80 value_log(); 81 delay(250); 82 } 83 if (digitalRead(subPin) == LOW) 84 { 85 setpoint--; 86 value_log(); 87 delay(250); 88 } 89 // Time between electromagnet state changes; 90 delayMicroseconds(dlyVal); 91} 92//---------------------------------------------------------| 93void value_log() 94// Analogic/Digital/Levitation Values Print; 95{ 96 // Show the Hall Sensor Value; 97 Serial.print("anaVal=["); 98 Serial.print(anaVal); 99 Serial.print("]-"); 100 // Show the Electromagnet state On=1/Off=0; 101 Serial.print("digVal=["); 102 Serial.print(digVal); 103 Serial.print("]-"); 104 // Show the Levitation Point Value; 105 Serial.print("setpoint=["); 106 Serial.print(setpoint); 107 Serial.println("];"); 108} 109//=========================================================| 110
Levitator.ino
arduino
Code for Magnet Levitation toy with Arduino. Easy(No PID)
1//=========================================================| 2// Ekobots 3 Innovation Ltda - www.ekobots.com.br | 4// Juan Sirgado y Antico 5 - www.jsya.com.br | 6//---------------------------------------------------------| 7// 8 Program Magnetic Levitator - 2016/02/06 | 9// All 10 rights reserved 2016 | 11//=========================================================| 12int 13 anaPin = 1; // Arduino Analogic Pin 1; 14int digPin = 5; // Arduino Digital 15 Pin 5; 16int subPin = 7; // Arduino Digital Pin 7; 17int addPin = 8; // 18 Arduino Digital Pin 8; 19// 20int anaVal = 0; // Analogic Read(Hall Sensor) 21 Value; 22boolean digVal = 0; // Digital Write(Electromagnet) Value; 23// 24int 25 levVal = 228; // Levitation Poit Value; 26int dlyVal = 250; // Delay Value 27 Micro Seconds; 28//---------------------------------------------------------| 29void 30 setup() 31{ 32 // Levitator initialization Begin; 33 Serial.begin(57600); 34 35 Serial.println("Levitator by JSyA"); 36 Serial.println("Starting..."); 37 38 // Digital Pins Work Mode Setup; 39 pinMode(digPin, OUTPUT); 40 pinMode(subPin, 41 INPUT_PULLUP); 42 pinMode(addPin, INPUT_PULLUP); 43 // Levitator initialization 44 End; 45 Serial.println("Started."); 46} 47//---------------------------------------------------------| 48void 49 loop() 50{ 51 // Hall Sensor Read (Magnetic Field Intensity); 52 anaVal 53 = analogRead(anaPin); 54 // Increase The Value Of Levitation Point; 55 if 56 (digitalRead(addPin) == LOW) 57 { 58 levVal++; 59 value_log(); 60 61 delay(250); 62 } 63 // Decrease The Value Of Levitation Point; 64 if 65 (digitalRead(subPin) == LOW) 66 { 67 levVal--; 68 value_log(); 69 70 delay(250); 71 } 72 // Check the Levitation Point; 73 if (anaVal 74 < levVal) 75 { 76 digVal = LOW; 77 } 78 else // if (anaVal > levVal) 79 80 { 81 digVal = HIGH; 82 } 83 // 84 // Turn ON/OFF The Electromagnet; 85 86 // With Base on Sensor Value and the Levitation Point; 87 digitalWrite(digPin, 88 digVal); 89 delayMicroseconds(dlyVal); 90} 91//---------------------------------------------------------| 92void 93 value_log() 94// Analogic/Digital/Levitation Values Print; 95{ 96 // Show the 97 Hall Sensor Value; 98 Serial.print("anaVal=["); 99 Serial.print(anaVal); 100 101 Serial.print("]-"); 102 // Show the Electromagnet state On=1/Off=0; 103 104 Serial.print("digVal=["); 105 Serial.print(digVal); 106 Serial.print("]-"); 107 108 // Show the Levitation Point Value; 109 Serial.print("levVal=["); 110 Serial.print(levVal); 111 112 Serial.println("];"); 113} 114//=========================================================| 115
Levitator_PID.ino
arduino
Code for Magnet Levitation toy with Arduino. Hard(PID version)
1//=========================================================| 2// Ekobots 3 Innovation Ltda - www.ekobots.com.br | 4// Juan Sirgado y Antico 5 - www.jsya.com.br | 6//---------------------------------------------------------| 7// 8 Program Magnetic Levitator PID - 2016/10/06 | 9// All 10 rights reserved 2016 | 11//=========================================================| 12int 13 anaPin = 1; // Arduino Analogic Pin 1 14int digPin = 5; // Arduino Digital 15 Pin 5 16int subPin = 7; // Arduino Digital Pin 7 17int addPin = 8; // Arduino 18 Digital Pin 8 19// 20int anaVal = 0; // Analogic Valie 21int digVal = 0; // 22 Digital Value 23// 24int levVal = 262; // Levitation Value 25int dlyVal = 10; 26 // Delay Value 27// 28float timVal = 0; // Time Value 29//---------------------------------------------------------| 30// 31 PID Values 32float setpoint = 0; 33float measured_value = 0; 34float output = 35 0; 36float integral = 0; 37float derivative = 0; 38float error = 0; 39float 40 previous_error = 0; 41float dt = 0.1; 42float Kp = 1.0; 43float Ki = 0.1; 44float 45 Kd = 0.01; 46//---------------------------------------------------------| 47void 48 setup() 49{ 50 // Levitator initialization Begin; 51 Serial.begin(57600); 52 53 Serial.println("Levitator by JSyA"); 54 Serial.println("Starting..."); 55 56 // Digital Pins Work Mode Setup; 57 pinMode(digPin, OUTPUT); 58 pinMode(addPin, 59 INPUT_PULLUP); 60 pinMode(subPin, INPUT_PULLUP); 61 // 62 timVal = millis(); 63 64 setpoint = levVal; 65 // Levitator initialization End; 66 Serial.println("Started."); 67} 68//---------------------------------------------------------| 69void 70 loop() // PID 71{ 72 // Hall Sensor Read (Magnetic Field Intensity); 73 anaVal 74 = analogRead(anaPin); 75 // PID calculations 76 measured_value = anaVal; 77 78 error = setpoint - measured_value; 79 integral = integral + error * dt; 80 81 derivative = (error - previous_error) / dt; 82 output = (-Kp * error) + (-Ki 83 * integral) + (-Kd * derivative); 84 previous_error = error; 85 // Final value 86 setup 87 digVal += output; 88 // Check the value for levitation point; 89 90 if (digVal < 0) digVal=0; 91 if (digVal > 255) digVal=255; 92 // Increase/Decrease 93 the value for Electromagnet; 94 // With Base on Sensor Value and the Levitation 95 Point; 96 analogWrite(digPin, digVal); 97 // Show log values for debug; 98// 99 if((millis()-timVal) > 500) 100// { 101// value_log(); 102// timVal 103 = millis(); 104// } 105 // Increase The Value Of Levitation Point; 106 if 107 (digitalRead(addPin) == LOW) 108 { 109 setpoint++; 110 value_log(); 111 112 delay(250); 113 } 114 if (digitalRead(subPin) == LOW) 115 { 116 setpoint--; 117 118 value_log(); 119 delay(250); 120 } 121 // Time between electromagnet 122 state changes; 123 delayMicroseconds(dlyVal); 124} 125//---------------------------------------------------------| 126void 127 value_log() 128// Analogic/Digital/Levitation Values Print; 129{ 130 // Show the 131 Hall Sensor Value; 132 Serial.print("anaVal=["); 133 Serial.print(anaVal); 134 135 Serial.print("]-"); 136 // Show the Electromagnet state On=1/Off=0; 137 138 Serial.print("digVal=["); 139 Serial.print(digVal); 140 Serial.print("]-"); 141 142 // Show the Levitation Point Value; 143 Serial.print("setpoint=["); 144 Serial.print(setpoint); 145 146 Serial.println("];"); 147} 148//=========================================================| 149
Downloadable files
Board Schematic
Fritzing IDE
Board Schematic
Comments
Only logged in users can leave comments