About 90 percent of hay is baled in most countries of the world. In Iran it is mainly cubic bales, which is made by baler. In addition to the time of harvest, the degree of compression of alfalfa bales is strongly depends on the maturity, time of cutting and its moisture content. So the operator should constantly control the moisture content and the degree of compactness of the bales. Otherwise, thebalescould be packed densely which could cause molding, or they could be compressed loosely which increases the expenses of traortation and storage due low density. Therefore, developing a controller which could adjust the density of bales based on theirmoisturecontent will be useful. In the design of the controller, the following features were considered: 1) to be economic 2) to be simple and 3) to be user-friendly. The systemconsisted of 1) moisture sensor, 2) hydraulic system, 3) control system, 4) rotational displacement sensor, and 5) a mechanism for changing the distance between the compressing plates of the bale chamber.The moisture sensor wasmeasuredusing anultrasonic sensor. This sensor wasconsisted of an ultra-sonic sender and receiver (40 kHz), a waveguide pipe with a 10 centimeter interior diameter,, and a 30 centimeter length. Waveguide pipe waslocated vertically on the hay bale. An ultra-sonic wave was produced by a, micro control (AT Mega 32)wassent by the ultrasonicsender inside the waveguide pipe, and after contact with the hay bale, the reflected wave was received by the ultrasonic receiver. The intensity of the received waves is related to the temperature and the moisture content of the hay. A temperature sensor was used for removing the influence of the temperature on the measured moisture content. The, hydraulic system consisted of asolenoidoperateddirectionalcontrol valve, a pressure reducing valve, and a hydraulic cylinder. The function ofthehydraulic system wasto move the mechanism for changing the distance between plates of the bale chamber. The control system was designed for processing the received data from moisture sensor and rotational displacement sensor, and controlling thethe directional control valve. The rotational displacement sensor wasdesignedto measure the amount of movement of the plates of bale chamber.A mechanism wasset up for changing the distance between the plates and coordinating the movement between them on the baler. The moisture sensor was calibrated in different temperatures and moisture range of 12-30% (db) with the coefficient of determination R 2 =0.98. The automatic control system for controlling the degree of rectangular bales compactnesswasassessed in a moisture range of 14-23%. There was no significant difference between the density of the bales made using the designed controlled system and the expected density. So, the bales with the desired density could be made by usingthe developed system. Keyword : Hay, density, moisture sensor, Ultrasonic sensor, baler